将 .NET XmlSerializer 与 get 属性和 setter 函数结合使用
我正在尝试使用 C# 中的 XmlSerializer 来保存一个类,该类具有一些由属性读取的值(代码只是字段值的简单检索),但由 setter 函数设置(因为有是一个在值发生变化时调用的委托)。
我目前正在做的就是这样的事情。预期用途是使用 InT
属性读取值,并使用 SetInT
设置它。设置它会产生副作用,因此这里方法比属性更合适。 XmlSerializationOnly_InT
的存在只是为了 XmlSerializer
(因此得名),并且不应由普通代码使用。
class X
{
public double InT
{
get { return _inT; }
}
public void SetInT(double newInT)
{
if (newInT != _inT)
{
_inT = newInT;
Changed();//includes delegate call; potentially expensive
}
}
private double _inT;
// not called by normal code, as the property set is not just a simple
// field set or two.
[XmlElement(ElementName = "InT")]
public double XmlSerializationOnly_InT
{
get { return InT; }
set { SetInT(value); }
}
}
这可行,而且很容易做到,而且 XML 文件看起来就像您所期望的那样。虽然是体力劳动,而且有点难看,所以我只是有点满意。我真正想要的是能够告诉 XML 序列化使用属性读取值,并使用 setter 函数设置它。那么我根本就不需要 XmlSerializationOnly_InT
。
我似乎通过这种方式区分属性集和设置器函数来遵循标准实践,所以我确信我不是唯一遇到这种情况的人(尽管谷歌建议我可能是)。其他人在这种情况下做了什么?是否有一些简单的方法可以说服 XmlSerializer 更好地处理此类事情?如果没有,是否还有其他简单的方法可以做到这一点?
I'm trying to use XmlSerializer
from C# to save out a class that has some values that are read by properties (the code being just a simple retrieval of field value) but set by setter functions (since there is a delegate called if the value changes).
What I'm currently doing is this sort of thing. The intended use is to use the InT
property to read the value, and use SetInT
to set it. Setting it has side-effects, so a method is more appropriate than a property here. XmlSerializationOnly_InT
exists solely for the benefit of the XmlSerializer
(hence the name), and shouldn't be used by normal code.
class X
{
public double InT
{
get { return _inT; }
}
public void SetInT(double newInT)
{
if (newInT != _inT)
{
_inT = newInT;
Changed();//includes delegate call; potentially expensive
}
}
private double _inT;
// not called by normal code, as the property set is not just a simple
// field set or two.
[XmlElement(ElementName = "InT")]
public double XmlSerializationOnly_InT
{
get { return InT; }
set { SetInT(value); }
}
}
This works, it's easy enough to do, and the XML file looks like you'd expect. It's manual labour though, and a bit ugly, so I'm only somewhat satisfied. What I'd really like is to be able to tell the XML serialization to read the value using the property, and set it using the setter function. Then I wouldn't need XmlSerializationOnly_InT
at all.
I seem to be following standard practise by distinguishing between property sets and setter functions in this way, so I'm sure I'm not the only person to have encountered this (though google suggests I might be). What have others done in this situation? Is there some easy way to persuade the XmlSerializer
to handle this sort of thing better? If not, is there perhaps some other easy way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
我真的只想让 setter 函数成为属性的一部分。如果您愿意,可以让设置器触发 事件 并在事件中调用该函数。
否则,您可以调整自己的序列化和反序列化函数:
EDIT:
I would really just make the setter function part of the property. If you want you could have the setter trigger an event and call the function in the event.
Otherwise you could just right your own serialize and deserialize functions: