Conditional Property Setters (friends in C#)
-
Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006
-
Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006
Well, I came across a way of doing this by accident sometime ago while I was exploring something else. However, I can't remember exactly what I did now! :(( Some combination of attributes and reflection. Kevin
-
Is there a way to designate a property as only being usable say, during serialization? For example, I need a setter that sets the string value during deserialization, but under no other condition should this setter ever be used. I really miss the "friend" feature of C++. [edit]I'm using C# 2.0, if that helps.[/edit] Marc Pensieve -- modified at 14:00 Saturday 11th February, 2006
If you are using binary serialization, the visibility of the property doesn't matter, because the serializer uses the (private) fields. Xml serialization is different. You could use the OnDeserialized attribute to execute a method whoch sets a boolean flag. In the setter, check the flag and throw an exception.