将 XML 可序列化公共字段重构为自动属性是否会破坏旧实例的反序列化?

发布于 2024-12-09 16:30:45 字数 290 浏览 1 评论 0原文

我有一个类,其中有许多标有 XML 可序列化属性的公共字段:

    [XmlAttribute]
    public string ExampleField = string.Empty;

我打算将这些字段(并且仅这些字段)重构为自动属性:

    [XmlAttribute]
    public string ExampleField { get; set; }

反序列化使用之前创建的实例是否会出现任何问题课程的版本?

I have a class which has a number of public fields that are marked with XML serializable attributes:

    [XmlAttribute]
    public string ExampleField = string.Empty;

I intend to refactor these fields (and only these fields) as auto-properties:

    [XmlAttribute]
    public string ExampleField { get; set; }

Will there be any issue with deserializing instances that were created using the previous version of the class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

亽野灬性zι浪 2024-12-16 16:30:45

它似乎工作得很好...

XmlSerializer我的 .Net Xml 序列化类的包装器。)

[Test]
public void XmlOutTest()
{
    var field = new Field { ExampleField = "TEXT" };
    var property = new Property() { ExampleField = "TEXT" };

    var fieldXml = XmlSerializer<Field>.Serialize(field);
    var propertyXml = XmlSerializer<Property>.Serialize(property);

    System.Console.WriteLine(fieldXml);
    System.Console.WriteLine(propertyXml);

    Assert.IsNotNull(XmlSerializer<Field>.Deserialize(propertyXml));
    Assert.IsNotNull(XmlSerializer<Property>.Deserialize(fieldXml));
}

public class Field
{
    public string ExampleField = string.Empty;
}

// changing root so I can use each other's xml when deserializing
[System.Xml.Serialization.XmlRoot("Field")]
public class Property
{
    public string ExampleField { get; set; }
}

输出:

<?xml version="1.0" encoding="utf-16"?>
<Field>
  <ExampleField>TEXT</ExampleField>
</Field>
<?xml version="1.0" encoding="utf-16"?>
<Field>
  <ExampleField>TEXT</ExampleField>
</Field>

It appears to work just fine...

(XmlSerializer is my wrapper to the .Net Xml Serialization classes.)

[Test]
public void XmlOutTest()
{
    var field = new Field { ExampleField = "TEXT" };
    var property = new Property() { ExampleField = "TEXT" };

    var fieldXml = XmlSerializer<Field>.Serialize(field);
    var propertyXml = XmlSerializer<Property>.Serialize(property);

    System.Console.WriteLine(fieldXml);
    System.Console.WriteLine(propertyXml);

    Assert.IsNotNull(XmlSerializer<Field>.Deserialize(propertyXml));
    Assert.IsNotNull(XmlSerializer<Property>.Deserialize(fieldXml));
}

public class Field
{
    public string ExampleField = string.Empty;
}

// changing root so I can use each other's xml when deserializing
[System.Xml.Serialization.XmlRoot("Field")]
public class Property
{
    public string ExampleField { get; set; }
}

Output:

<?xml version="1.0" encoding="utf-16"?>
<Field>
  <ExampleField>TEXT</ExampleField>
</Field>
<?xml version="1.0" encoding="utf-16"?>
<Field>
  <ExampleField>TEXT</ExampleField>
</Field>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文