Note that there are some explanatory texts on larger screens.

plurals
  1. PODataContractSerializer not Serializing member of class that inherits ISerializable
    text
    copied!<p>I have this class:</p> <pre><code>using System; using System.Collections.Generic; using System.Runtime.Serialization; namespace Grouping { [Serializable] public class Group&lt;T&gt; : HashSet&lt;T&gt; { public Group(string name) { this.name = name; } protected Group(){} protected Group(SerializationInfo info, StreamingContext context):base(info,context) { name = info.GetString("koosnaampje"); } public override void GetObjectData(SerializationInfo info,StreamingContext context) { base.GetObjectData(info,context); info.AddValue("koosnaampje", Name); } private string name; public string Name { get { return name; } private set { name = value; } } } } </code></pre> <p>As it inherits from HashSet it has to implement ISerializable, hence the protected constructor and GetObjectData method. Formerly I serialized and deserialized this class succesfully with the BinaryFormatter.</p> <p>Because I want to be able to inspect the output that is generated by the serializer I want to switch to the DataContractSerializer.</p> <p>I wrote this test:</p> <pre><code>[TestMethod] public void SerializeTest() { var group = new Group&lt;int&gt;("ints"){1,2,3}; var serializer = new DataContractSerializer(typeof (Group&lt;int&gt;)); using (var stream=File.OpenWrite("group1.xml")) { serializer.WriteObject(stream,group); } using (var stream=File.OpenRead("group1.xml")) { group = serializer.ReadObject(stream) as Group&lt;int&gt;; } Assert.IsTrue(group.Contains(1)); Assert.AreEqual("ints",group.Name); } </code></pre> <p>The test fails because the Name property is null! (the integers are (de)serialized correctly though) What is happening?</p> <p>EDIT: it has nothing to do with the name backing field being private. Making it public has the same result.</p>
 

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