进行单向序列化的正确方法
假设我有一个这样的类:
public class AlphaBravoRatio
{
public int A {get; set;}
public int B {get; set;}
public double Ratio { get { return A * 1.0 / B; } }
}
有没有办法装饰这个类,使得 Ratio 被序列化但不被反序列化?
它适用于这个实现,但我想知道是否可以在没有空设置器的情况下完成。
public class AlphaBravoRatio
{
public int A {get; set;}
public int B {get; set;}
public double Ratio
{
get { return A * 1.0 / B; }
set {}
}
}
Say I have a class like this:
public class AlphaBravoRatio
{
public int A {get; set;}
public int B {get; set;}
public double Ratio { get { return A * 1.0 / B; } }
}
Is there a way to decorate this class such that Ratio gets serialized but not deserialized?
It works with this implementation but I'm wondering if it can be done without the empty setter.
public class AlphaBravoRatio
{
public int A {get; set;}
public int B {get; set;}
public double Ratio
{
get { return A * 1.0 / B; }
set {}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现 IXmlSerialized 并仅实现 WriteXml方法。
You could implement IXmlSerializable and only implement the WriteXml method.