How bad is something like:
public class Test
{
private string pKey = null;
public string Key {
get { return pKey; }
set { if (pKey==null) pKey=value;}
}
}
This would allow me to use XMLSerializer with the class and make sure that Key can’t be changed after being initially set.
I agree that my initial idea was bad.
I now know that there is no way to make this using the standard XML Serializer. ‘ssg’ suggestion won’t be serialized because it doesn’t have a public setter.
The only choices here are implementing the
IXmlSerializable, or using another serialization method, likeDataContractSerializer. The problem with the former is that every derivate of the class would also have to implementIXmlSerializable; the problem with the latter is that you can’t use attributes or have much control over the generated XML.