I need to make a class Serializable.
In this class I have a MyGuid readonly property, that I want to be serializable but not deserializable (property is initialized in a backing field).
Using base serialization .NET features, you know, have a reandonly property make deserialization fail, ’cause it cannot deserialize a readonly property.
So I decide to create a public get-set MyGuid property with a backing field, and make setter do nothing:
[Serializable]
public class Task : ITask
{
private readonly Guid m_guid = Guid.NewGuid();
public MyGuid Guid
{
get { return m_guid; }
set { /*Empty setter!*/ }
}
}
Now I don’t want to shoot in my foot…
There’s a way I can make setter of MyGuid property as “deprecable” or “disabled”?
It would be nice have Visual Studio warning me if I try to use the setter.
Or, instead, there’s a better way to manage this kind of needs?
Thank you!
Edit: Found something here: Serializing private member data I’m reading…
This if you accidentally set the value yourself, you’ll get a warning in the debug window. On the other hand, you absolutely need the setter for the serializer, or otherwise the deserializer would never be able to appoint the property a value after reading it from the file! So the setter is not for you but for the serializer to function normally.