Pb Serialization with property without set

  • Thread starter Thread starter Shawn Wildermuth
  • Start date Start date
S

Shawn Wildermuth

Serialization is meant to be bidirectional, so unless your object can be
set, the XML Serializer doesn't think it can re-hydrate your object so it
doesn't serilaize it. BTW, [Serializable] isn't required if you're using
XmlSerilization...they are two entirely different serialization strategies
(XML Serialization vs. std serializaition).

HTH

Shawn Wildermuth
http://adoguy.com
 
Question :

Why property without set is not serialized ?

Code :

[Serializable()]
public class Test
{
private int id;

public Test()
{
id = 0;
}

public int Id
{
get { return id; }
// Without set
}
}

Test test = new Test();

XmlSerializer formatter = new XmlSerializer(typeof(Test));
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms,test);
string result = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
ms.Close();

result = "<Test/>"

Marc
 
Back
Top