Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To correctly serialize a List structure, you have to use the <strong>CollectionDataContract</strong> attribute like so:</p> <pre><code> [CollectionDataContract] [Serializable] internal class ComplexObject&lt;T&gt; : List&lt;T&gt;, IEquatable&lt;ComplexObject&lt;T&gt;&gt; where T : IEquatable&lt;T&gt; </code></pre> <p>However, the CollectionDataContract doesn't allow for additional DataMembers to be serialized. The workaround would be to avoid inheriting from the list, but make it a member variable instead and optionally implement the ICollection, like so:</p> <pre><code>[DataContract] [Serializable] internal class ComplexObject&lt;T&gt; : ICollection&lt;T&gt;, IEquatable&lt;ComplexObject&lt;T&gt;&gt; where T : IEquatable&lt;T&gt; { private T m_state; [DataMember] public T State { get { return m_state; } set { m_state = value; } } private List&lt;T&gt; m_List = new List&lt;T&gt;(); [DataMember] public List&lt;T&gt; List { get { return m_List; } set { m_List = value; } } public bool Equals(ComplexObject&lt;T&gt; other) { if (!other.State.Equals(State)) return false; if (other.List.Count != List.Count) return false; for (int i = 0; i &lt; other.List.Count;i++) { if (!other.List[i].Equals(List[i])) return false; } return true; } // ICollection members left out to save space // helper methods to wrap around the List to decrease the amount // of refactoring work you would have to do public void Add(T item) { List.Add(item); } public bool Remove(T item) { return List.Remove(item); } public T this[int index] { get { return List[index]; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload