how to get a copy of ref type object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

in .net, if you set annstance-A of a class equal to another instance-B, a
pointer will add to B, but if i want to create a copy of B instead of
pointer, how to operate?

Note:serialization isn't permitted

3x
 
hi
Take a look at
Object.MemberwiseClone Method

[MSDN]
This method cannot be overridden; a derived class should implement the
ICloneable interface if a shallow copy is not appropriate. MemberwiseClone is
protected and, therefore, is accessible only through this class or a derived
class.

A shallow copy creates a new instance of the same type as the original
object, and then copies the non-static fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object is
not; therefore, the reference in the original object and the reference in the
clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.

For example, if X is an Object with references to the objects A and B, and
the object A also has a reference to an object M, a shallow copy of X is an
object Y, which also has references to objects A and B. In contrast, a deep
copy of X is an object Y with direct references to objects C and D, and an
indirect reference to object N, where C is a copy of A, D is a copy of B, and
N is a copy of M.

[/ MSDN]

this is the example in msdn

The following code example shows how to copy an instance of a class using
MemberwiseClone.

[C#]
using System;

class MyBaseClass {
public static string CompanyName = "My Company";
public int age;
public string name;
}

class MyDerivedClass: MyBaseClass {

static void Main() {

// Creates an instance of MyDerivedClass and assign values to its fields.
MyDerivedClass m1 = new MyDerivedClass();
m1.age = 42;
m1.name = "Sam";

// Performs a shallow copy of m1 and assign it to m2.
MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
}
}

regards
Ansil
 
As the documentation below suggests - MemberwiseClone does a Matrix like
copy, but what the guy needs is a Star Trek teleporting like copy. So it
won't work.

And the documentation says - "implement ICloneable" .. well okay but what in
the world will you write in the .Clone() implementation.

The easiest way I must say is Serialization - but as you say that is not
permitted - there are 2 more ways to do so -

a) Painfully copy over each property of the object.
b) Use Win32, Kernel32.dll 's MoveMemory function to copy over the
bytestructure of your object - then use unsafe pointer code to reference to
the new memory location.

The catch 22 in approach b is, you need to know the size of the object
before you use that, and well - to know the size you're best off serializing
to a memorystream and checking the length on it ..

So we're back to square 1.

Why not serialization? That's the mostest straightforwadestet way;
especially when you don't know the object structure.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik





Ansil MCAD said:
hi
Take a look at
Object.MemberwiseClone Method

[MSDN]
This method cannot be overridden; a derived class should implement the
ICloneable interface if a shallow copy is not appropriate. MemberwiseClone
is
protected and, therefore, is accessible only through this class or a
derived
class.

A shallow copy creates a new instance of the same type as the original
object, and then copies the non-static fields of the original object. If
the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is
not; therefore, the reference in the original object and the reference in
the
clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in
the
object.

For example, if X is an Object with references to the objects A and B, and
the object A also has a reference to an object M, a shallow copy of X is
an
object Y, which also has references to objects A and B. In contrast, a
deep
copy of X is an object Y with direct references to objects C and D, and an
indirect reference to object N, where C is a copy of A, D is a copy of B,
and
N is a copy of M.

[/ MSDN]

this is the example in msdn

The following code example shows how to copy an instance of a class using
MemberwiseClone.

[C#]
using System;

class MyBaseClass {
public static string CompanyName = "My Company";
public int age;
public string name;
}

class MyDerivedClass: MyBaseClass {

static void Main() {

// Creates an instance of MyDerivedClass and assign values to its
fields.
MyDerivedClass m1 = new MyDerivedClass();
m1.age = 42;
m1.name = "Sam";

// Performs a shallow copy of m1 and assign it to m2.
MyDerivedClass m2 = (MyDerivedClass) m1.MemberwiseClone();
}
}

regards
Ansil



lion said:
in .net, if you set annstance-A of a class equal to another instance-B,
a
pointer will add to B, but if i want to create a copy of B instead of
pointer, how to operate?

Note:serialization isn't permitted

3x
 
Sahil Malik said:
As the documentation below suggests - MemberwiseClone does a Matrix like
copy, but what the guy needs is a Star Trek teleporting like copy.

We don't know that. We know he doesn't want a copy of the reference
itself, as assignment gives, but a shall copy may be fine.
So it won't work.

And the documentation says - "implement ICloneable" .. well okay but what in
the world will you write in the .Clone() implementation.

You use MemberwiseClone for everything which you don't need a deep copy
of, and you need to implement something to do the deep copy for those
members which require a bit more.
The easiest way I must say is Serialization - but as you say that is not
permitted - there are 2 more ways to do so -

a) Painfully copy over each property of the object.
b) Use Win32, Kernel32.dll 's MoveMemory function to copy over the
bytestructure of your object - then use unsafe pointer code to reference to
the new memory location.

You've got to do that recursively if you want to do a *really* deep
copy - otherwise it's just the same as using MemberwiseClone.
 
Sahil Malik said:
If the object is serializable, you could serialize into a memorystream and
deserialize to a new instance and in essence get the same effect that
recursion would have given you.

Sure. However, we were specifically told that serialization wasn't an
acceptable answer. (It would have helped if we'd been told why,
admittedly...)
 
Back
Top