I have a serializable class and one of the properties in my class generates a Guid
in the getter. The property implements no setter and is ignores during serialization. Why is that and do I always have to implement a setter in order for my property to be serialized.
[Serializable]
public class Example
{
[XmlAttribute("id")]
public string Id
{
get
{
return Guid.NewGuid().ToString();
}
}
}
I tried implementing an empty setter and it got serialized correctly.
[Serializable]
public class Example
{
[XmlAttribute("id")]
public string Id
{
get
{
return Guid.NewGuid().ToString();
}
set {}
}
}
Update:
Can you point out how should I define properties whose values never change or ones that the value for is generated internally?