Improve Visual Studio warning when using empty properties Configuration Tool

I need to make Serializable class. In this class, I have a readonly MyGuid property that I want to be serializable but not deserializable (the property is initialized in the support field). You know that with the basic .NET serialization functions, you have the reandonly property so that deserialization fails because it cannot deserialize the readonly property. So I decided to create shared MyGuid property with a given field and to make the setter to 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 the foot ... Is there a way to make the installer of the MyGuid property “disgusting” or “disabled”? It would be nice if Visual Studio will warn me if I try to use the installer.

Or, instead, there is a better way to deal with these needs?

Thanks!

Edit: I found something here: Serialization data private party I read ...

+4
source share
2 answers
 public Guid Guid { get { return m_guid; } set { if (value != null) Debugger.Log(0, "Warning", "This property has an empty setter, just for serializing purpose!"); } } ) Debugger.Log ( public Guid Guid { get { return m_guid; } set { if (value != null) Debugger.Log(0, "Warning", "This property has an empty setter, just for serializing purpose!"); } } has an empty setter, just for serializing purpose!"); public Guid Guid { get { return m_guid; } set { if (value != null) Debugger.Log(0, "Warning", "This property has an empty setter, just for serializing purpose!"); } } 

It is, if you accidentally set the value yourself, you will get a warning in the debug window. On the other hand, you absolutely need the installer to the serializer, deserializer otherwise never be able to assign the value of the property after reading from a file! Thus, the installer is not for you, but in order to serializer functioned normally.

+2
source

If you do not turn on the set, it will automatically recognize it as read-only, so do not let it install. Then he will celebrate an error in your code when you try to

0
source

All Articles