开发者

How To Deserialize Missing Boolean Field to TRUE

I'm deserializing a custom object from a file to an object in my app using the XmlSerializer. My issue is that I want a field in the object to default to "True" rather than "False" for a new property that doesn't exist in the file that I am deserializing from.

By default, .Net is assigning this value to be false because it doesn't exist in the file and I want it to default to True if it doesn't exist. I used the System.ComponentModel.DefaultValue(True) attribute on the field in the definition of the object, but that didn't work. Does 开发者_运维知识库anyone know how to do this?


You can set the value of the boolean in the empty constructor of your object directly. This way the deserializer will create the deserialized object with true in the boolean.


For some reason this didn't work for me when I was deserializing a field that was another object class that had a boolean property. Maybe I missed something but I just made the boolean nullable (bool?) and after deserializing, set the boolean to true if it was null.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜