开发者

object assignment

I have a scenario like:

MyClass obj1 = new MyClass();
............//some operations on obj1;
MyClass obj2 = new MyClass();
obj2 = obj1;

I have the following problem: if I modify any parameter, it is affected in both objects (as both refer to same location) - but, when I modify obj2 parameter, it should not modify that parameter value in obj1 (i.e. m开发者_运维问答eans both should not point to same location). How can I do that? Please help me. I can't clone here as myclass is not implementing ICloneable and I can't modify myclass. if I clone by serializing and deserializing, will it be a Deep clone?


Make your MyClass implement ICloneable and use

MyClass obj1 = new MyClass();
...
MyClass obj2 = obj1.Clone();

If MyClass is not clonable, you need to look up all characteristic values in obj1 and copy them to obj2, e.g.

myclass obj2 = new myclass();
obj2.color = obj1.color; // .Clone();
obj2.size = obj1.size;
obj2.numberOfLimbs = obj1.numberOfLimbs;
// etc.


The thing to remember with object assignment is the difference between variables and objects.

In your example, obj1 and obj2 are variables. Variables can refer to objects, but they are not objects themselves.

What your code does is, at the end, tell both obj1 and obj2 to refer to the same object.

What you want to do is make a new object - which, as others pointed out, is most easily done through the ICloneable interface.


Have an extension method on myClass : GetDeepCopy Manually get make a copy of the obj and return this in GetDeepCopy.

So something like :

myclass obj1 = new myclass();

...

myclass obj2 = obj1.etDeepCopy();


To add to KennyTM's answer, object Clone() method makes a copy of the invoking object. There are two type of copies which can be made. Deep copy and shallow copy. In KennyTM's answer, a deep copy is made. In a deep copy, the original object and the copied object are completely independent of each other. For more info, read up on the docs for ICloneable.

And the Clone() declaration could be something like this:

public object Clone()
{ 
 Myclass obj=new Myclass();
 return obj;
}


Assuming that the types on the object are simple, could you simply write a function that performs a kind of MemberwiseClone e.g.

MyClass obj = new MyClass();
// do your thing
MyClass objCopy = new MyClass();
objCopy.IamInt = obj.IamInt;
objCopy.IamString = obj.IamString;

Also more generally I found this Jon Skeet article very helpful when considering referencing.


KennyTM's suggestion would be the default choice. However, since you can't modify the source as you mentioned in a comment, you may need to write much code, possibly reflection if private members are involved.

If you can use an open-source library and if all types in the object graph of the to-be-cloned object have a default constructor, you can check out a utility in my library: Fasterflect's DeepClone(). This utility performs, no surprise, deep clone and handles cyclic references; the implementation is backed by CIL code generation, so no performance should be much better than hand-crafted reflection code.


If MyClass declares a copy constructor, you could do a

MyClass obj2=new MyClass(obj1).

Otherwise, you should create a function to copy as in:

MyClass CopyMyClassObject(MyClass obj1)
{
    MyClass Result = new MyClass();
    Result.Value1 = obj1.Value1;
    Result.Value2 = obj1.Value2;
    //...
    Result Valuen = obj1.Valuen;
    Result.Object1.Value1 = obj1.Object1.Value1;
    Result.Object1.Value2 = obj1.Object1.Value2;
    //...
    Result.Object1.Valuen = obj1.Object1.Valuen;
    //..and so on until all values have been assigned
    //The actual assignments will use whatever methods are provided in MyClass, of course.
    return Result;
}

After that, in your code you would simply do:

MyClass obj2 = CopyMyClassObject(obj1);

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜