构造的C#XML序列化:公共设置器/获取器和私有字段 [英] C# XML Serialization of construct: public setter/getter and private field

查看:72
本文介绍了构造的C#XML序列化:公共设置器/获取器和私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.
我为此有主要指标(_measurePercent)和公共获取者/设置者(MeasurePercent).

Hello.
I have my main measure (_measurePercent) and public getter/setter (MeasurePercent) for this.

private double _measurePercent;
public double MeasurePercent
{
    get
    {
        if (double.IsNaN(_measurePercent) || double.IsInfinity(_measurePercent))
           throw new Exception("...");
        return _measurePercent;
    }
    set
    {
        if(value > 100.0f || value < 0.0f)
           throw new Exception("...");
        _measurePercent = value;
    }
 }



我想在XML中将此值序列化.
我知道XML序列化不适用于私有字段,因此getter/setter上的[XmlIgnore]将无济于事.
getter/setter的序列化也不起作用,因为它使用了_measurePercent,该值未反序列化.
我想问一问,是否有可能在不编写自己的XML序列化器的情况下使其工作.



I want to serialize in XML this value.
I know that XML Serialization doesn''t work for private fields, so [XmlIgnore] on getter/setter won''t help.
Serialization of getter/setter also doesn''t work, because it uses _measurePercent which value isn''t deserialized.
I want to ask, if it is posible to make this to work without writing my own XML Serializer.

推荐答案

这应该可以很好地进行序列化,因为序列化可以使用公共属性.当然,请意识到,由于您的属性会引发异常,因此在序列化对象时可能会引发相同的异常.
This should serialize just fine, since serialization works with public properties. Realize, of course, that since your properties can throw exceptions, those same exceptions may be thrown while the object is being serialized.


这篇关于构造的C#XML序列化:公共设置器/获取器和私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆