I have some types I want to serialize as xml, but these types have read-only properties like:
public List<Effect> Effects {get; private set;}
but the xml serializer re开发者_运维技巧quires these properties to be writable.
Isn't xml serializer using reflection, so in effect can easily set these properties via reflection even though they are read-only?
Is there a way around this because I don't want these types to be editable by people, so the properties must be read-only, but I also want them to be xml serializeable.
Its not possible because As mentioned in MSDN
XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport.
But you can use DataContractSerializer. Here is a link to Marc's Answer on SO
Serializing private member data
Update
You can get over that behavior by leaving Auto Implemented properties and have somthing like this:
private List<Effect> _Effects;
public Effect()
{
_Effects= new List<Effects>();
}
public List<Effect> Effect
{
get
{
return _Effects;
}
}
精彩评论