Hi Joe,
So far I understand your scenario that you want to implement the Copy/Clone
via Serialize the object into a memorystream and deserialize it back to a
new object.
Now you want to leverage the .NET Serialization feature to customize that
it will not serialize the objects in the collection.
Based on my research, the .NET serialization did not have such feature to
exactly reach your goal.
1. We have an attribute NonSerializedAttribute, which can be used to
indicates that a field of a serializable class should not be serialized.
This class cannot be inherited.
But in your scenario, the List we want to prevent from serializing is in
the CollectionBase build class but not our class.
NonSerializedAttribute Members
http://msdn2.microsoft.com/en-us/lib...ttribute_membe
rs.aspx
2. By implement ISerializable, we can control how to serialize an object by
implementing GetObjectData, but that methods works as the pattern building
from start.
e.g.
void ISerializable.GetObjectData(
SerializationInfo info, StreamingContext context) {
// Serialize the desired values for this class
info.AddValue("title", title);
// Get the set of serializable members for our class and base classes
Type thisType = this.GetType();
MemberInfo[] mi =
FormatterServices.GetSerializableMembers(thisType, context);
// Serialize the base class's fields to the info object
for (Int32 i = 0 ; i < mi.Length; i++) {
// Don't serialize fields for this class
if (mi[i].DeclaringType == thisType) continue;
info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this));
}
}
We need do all the job about what we want to serialize to a stream,
including the fields in the collectionbase.
3. So for your scenario, a simple resolution was to create a copy method
which will serialize the collection into memorystream and deserialize to a
new object and then clear the objects in collection.
If you have any concern, please feel free to let me know.
Here are some good article about serialization.
http://msdn.microsoft.com/msdnmag/is...t/default.aspx http://msdn.microsoft.com/msdnmag/issues/02/07/net/ http://msdn.microsoft.com/msdnmag/is...t/default.aspx
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.