XmlSerializer

问题描述

在子标题“XML 序列化注意事项”下at this link it says...

一个类必须有一个无参数的构造函数才能被序列化 XmlSerializer。

这实际上是一种误导。实际上,您不需要声明无参数构造函数认构造函数)。 stackoverflow.com 上的答案表明您不需要声明无参数构造函数,除非您声明了参数化构造函数。一旦声明了参数化构造函数,那么您还需要声明无参数构造函数

我对无参数构造函数语句块感兴趣。

我的测试程序表明,在一种情况下,将无参数构造函数留空是无害的,并且恢复成功。在另一种情况下,无参数构造函数中的成员数据分配似乎被恢复覆盖了。在另一种情况下,可以在无参数构造函数中扩充成员数据对象图,并且该扩充先于恢复的对象。也许规则是,如果成员数据是引用,如果引用为空,它将被恢复,但如果无参数构造函数语句块使其非空,那么恢复将增加该引用处的对象。 编辑:我刚刚意识到规则是错误的,因为实际上字符串是一种引用类型,而我的测试显示字符串引用无法恢复。

无参数构造函数语句块的规则是什么? (如果您知道记录在哪里,请发布链接。)

public class BlobEmpty
{
    public string name;
    public List<int> list;
    internal BlobEmpty() 
    {
        // demonstrate that XmlSerializer does not require any content in the 
        // statement block of the parameterless constructor
        Console.Beep(); // this gets executed upon deserialization 
                        // but this statement block is essentially empty
        // ** note that I do not instantiate the list of int **
    }
    internal BlobEmpty(string name) 
    { 
        this.list = new List<int>(); 
        this.name = name; 
    }
}
public class BlobPopulate
{
    public string name;
    public List<int> list;
    internal BlobPopulate()
    {
        Console.Beep(); // this gets executed upon deserialization
        this.list = new List<int>(); // this object will survive the restoration
        this.list.Add(999); // this item will be in front of the restored items
        this.name = "garbage"; // this string will not survive the restoration
    }
    internal BlobPopulate(string name)
    {
        this.list = new List<int>();
        this.name = name;
    }
}

为了完整起见,这里是测试程序...

private static void TestDeserialization0(string name,string fileName)
{
    var c = new BlobEmpty(name);
    c.list.Add(123); 
    c.list.Add(456);
    Serialize(c,fileName);
    var restored = (BlobEmpty)Deserialize(typeof(BlobEmpty),fileName);
    Debug.Assert(restored.name.Equals(name));
    Debug.Assert(restored.list.Count == 2);
    Debug.Assert(restored.list[0].Equals(123));
    Debug.Assert(restored.list[1].Equals(456));
}
private static void TestDeserialization1(string name,string fileName)
{
    var c = new BlobPopulate(name);
    c.list.Add(123);
    c.list.Add(456);
    Serialize(c,fileName);
    var restored = (BlobPopulate)Deserialize(typeof(BlobPopulate),fileName);
    Debug.Assert(restored.name.Equals(name));
    Debug.Assert(restored.list.Count == 3);
    Debug.Assert(restored.list[0].Equals(999));
    Debug.Assert(restored.list[1].Equals(123));
    Debug.Assert(restored.list[2].Equals(456));
}

解决方法

这实际上是一种误导。实际上,您不需要声明无参数构造函数(默认构造函数)。 stackoverflow.com 上的答案表明您不需要声明无参数构造函数,除非您声明了参数化构造函数。一旦声明了参数化构造函数,那么您还需要声明无参数构造函数。

这不是误导。在没有任何其他构造函数的情况下,C# 编译器会自动添加默认的无参数构造函数。它仍然是声明的,只是不是你声明的。


BlobEmpty 中,序列化程序将检查 list 并看到它是 null,因此它将实例化它。然后它会调用 Add 来插入新元素。

BlobPopulate 中,序列化程序将采用 list 并看到它不是 null。它将在现有列表上调用 Add 以插入新元素。

任何其他属性(不是 IEumerable)将始终被覆盖。