进行单向序列化的正确方法

发布于 2025-01-07 17:33:33 字数 480 浏览 4 评论 0原文

假设我有一个这样的类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2025-01-14 17:33:33

您可以实现 IXmlSerialized 并仅实现 WriteXml方法。

You could implement IXmlSerializable and only implement the WriteXml method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文