You don't need to avoid anything, ever. Your
Member1
and
Member2
are still different object and should be serialized, as soon as you have them in the contract. If they serve the same purpose by some weird reason, don't use the
[DataMember]
attribute for one of them.
A side advice: also use namespace URI for all [DataContract] attributes — it can make your data model's XML-based data format world-unique.
[EDIT]
Please see my comment below. If my guess is correct, you should understand everything, if you read on the
System.Runtime.Serialization.DataContractSerializer.PreserveObjectReferences
property or simply try it out:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer.preserveobjectreferences.aspx[
^].
Are you getting the idea? In particular, this is a very important feature is you want to persists the graph which are not trees. If this is the case, but you fail to use
true
value for this property, it may cause infinite recursion and, hence, stack overflow.
Again, to understand how it works in terms of XML format, try it out. It uses special references to IDs.
[END EDIT]
—SA