Re: Custom Surrogate
- From: "Joe" <jbassking@xxxxxxxxxxxxxxx>
- Date: Mon, 21 Aug 2006 09:46:02 -0400
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.
.
- Follow-Ups:
- Re: Custom Surrogate
- From: "Peter Huang" [MSFT]
- Re: Custom Surrogate
- References:
- Custom Surrogate
- From: Joe
- RE: Custom Surrogate
- From: "Peter Huang" [MSFT]
- Custom Surrogate
- Prev by Date: Re: UML Tool for C#
- Next by Date: Re: Freeze GUI
- Previous by thread: RE: Custom Surrogate
- Next by thread: Re: Custom Surrogate
- Index(es):
Relevant Pages
|