开发者

Why does Xml Serializer require properties to be writable?

开发者 https://www.devze.com 2023-02-12 14:02 出处:网络
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;}

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.

  1. Isn't xml serializer using reflection, so in effect can easily set these properties via reflection even though they are read-only?

  2. 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;         
     }  
 }  
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号