Re: Custom Surrogate

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Peter,

I need to do a deep copy of my object but I do NOT want to copy the objects
within the collection just the fields of the collection itself.
The reason I was thinking serialization is because it automatically does a
deep copy of all the objects. I just don't know how to use GetObjectData to
exclude the objects in the collection.

-Joe

""Peter Huang" [MSFT]" <v-phuang@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:DTcGyjPxGHA.5720@xxxxxxxxxxxxxxxxxxxxxxxx
Hi Joe,

Firstly I would like to check with you what do you mean by "Copy"/
Based on my research, the method GetObjectData/SetObjectData is used when
we are trying to serialize/deserialize an object to/from a stream.
Here is link for you reference.
ISerializable.GetObjectData Method
http://msdn2.microsoft.com/en-us/library/system.runtime.serialization.iseria
lizable.getobjectdata.aspx

You may check if the Copy method is what you want.
class Class1 : CollectionBase
{
public Class1 Copy()
{
Class1 oClass1 = this.MemberwiseClone();
oClass1.Clear();
}
}

NOTE: the MemberwiseClone will do a shadow copy.
For detailed information, please check the link below.
Remarks
The MemberwiseClone method creates a shallow copy by creating a new
object,
and then copying the nonstatic fields of the current object to the new
object. If a field is a value type, a bit-by-bit copy of the field is
performed. If a field is a reference type, the reference is copied but the
referred object is not; therefore, the original object and its clone refer
to the same object.

For example, consider an object called X that references objects A and B.
Object B, in turn, references object C. A shallow copy of X creates new
object X2 that also references objects A and B. In contrast, a deep copy
of
X creates a new object X2 that references the new objects A2 and B2, which
are copies of A and B. B2, in turn, references the new object C2, which is
a copy C. Use a class that implements the ICloneable interface to perform
a
deep or shallow copy of an object.

Object.MemberwiseClone Method
http://msdn2.microsoft.com/en-us/library/system.object.memberwiseclone.aspx

So it is important how will you deal with a field/property which is a
reference type(e.g. another collection), will you generate another copy of
the collection item? Is the MemberwiseClone method enough for your
scenario?


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.



.



Relevant Pages

  • Re: "deep copy" with VisualWorks?
    ... references or pointers to objects in the copied array. ... then I tried "dcopy," which seemed to do the trick, although it has ... some strange reference in the comment to creating a "smart deep copy," ...
    (comp.lang.smalltalk)
  • Re: How do u return a new object; an exact copy of the object which it is called?
    ... In a shallow copy, just copy the references. ... In a deep copy, make identical copies ... "To doo bee doo bee doo." ...
    (comp.lang.java.programmer)
  • Re: Deep Copying Techniques?
    ... Any arbitrary object may itself have references to objects that don't ... provide deep copies of themselves. ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... "Steve - DND" wrote in message ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: sizeof in java! need help
    ... It's trivial to write a reflection-based method that gets the deep size, but normally, you do not really want the deep size, but something between the flat size and the deep size. ... you want to know the size of the object itself plus the size of the referenced objects that are not referenced by other references. ...
    (comp.lang.java.help)
  • Re: Returning array: reference or deep copy?
    ... > Ryan Stewart wrote: ... >> Always references. ... If you want a copy (deep or shallow), ...
    (comp.lang.java.programmer)