c# - ISerializable 与递归 child

标签 c# arrays list recursion iserializable

我想为包含相似类型子项列表的 C# 类实现 ISerializable。考虑以下示例:

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace serialisation
{
    [Serializable]
    internal class Nested : ISerializable
    {
        public string Name { get; set; }

        public List<Nested> Children { get; set; }

        public Nested(string name)
        {
            Name = name;
            Children = new List<Nested>();
        }

        protected Nested(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        {
            Name = info.GetString("Name");

            // This doesn't work:
            Nested[] children = (Nested[])info.GetValue("Children", typeof(Nested[]));
            Children = new List<Nested>(children);

            // This works:
            // Children = (List<Nested>)info.GetValue("Children", typeof(List<Nested>));
        }

        public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
        {
            info.AddValue("Name", Name);

            // This doesn't work:
            info.AddValue("Children", Children.ToArray());

            // This works:
            //info.AddValue("Children", Children);
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            // Generate a hierarchy
            Nested root = new Nested("root");
            Nested child1 = new Nested("child1");
            Nested child2 = new Nested("child2");
            Nested child3 = new Nested("child3");
            child1.Children.Add(child2);
            child1.Children.Add(child3);
            root.Children.Add(child1);

            Nested deserialized;
            BinaryFormatter binaryFmt = new BinaryFormatter();

            // Serialize
            using (var fs = new FileStream("Nested.xml", FileMode.OpenOrCreate))
            {
                binaryFmt.Serialize(fs, root);
            }

            // Deserialize
            using (var fs = new FileStream("Nested.xml", FileMode.OpenOrCreate))
            {
                deserialized = (Nested)binaryFmt.Deserialize(fs);
            }

            // deserialized.Children contains one null child
            Console.WriteLine("Original Name: {0}", root.Name);
            Console.WriteLine("New Name: {0}", deserialized.Name);
        }
    }
}

在上面的示例中,Nested.GetObjectData 和 Nested 的序列化器构造函数被调用了 4 次,一个接一个。

将子项作为嵌套数组添加到序列化程序将在反序列化时返回正确大小的数组,但所有元素都将为空。

但是,将类型从 Nested array 更改为 Nested List 将在调用子元素的构造函数后神奇地修复空元素。

我想知道的是:

  1. 嵌套列表有什么特别之处?
  2. 推荐使用何种方法来序列化具有此类递归结构的类?

更新:

好像还有一个额外的接口(interface),IDeserializationCallback.OnDeserialization,在反序列化发生后调用(调用顺序是不确定的)。您可以将反序列化的数组存储在构造函数中的临时成员变量中,然后将其分配给此方法中的列表。除非我遗漏了什么,否则这似乎不太理想,因为您必须使用临时变量来扰乱您的实现。

最佳答案

我会选择复合模式。下面的解决方案解决了 BinaryFormatter(就像在您的 Main 中一样)和 XmlSerializer 方法,如果您改用它的话。 CompositeComponent 替换您的 Nested 类。

[Serializable()]
[XmlRoot("component", Namespace="", IsNullable=false)]
public partial class CT_Component 
{
    [XmlAttribute("name")]
    public string Name { get; set;}
}

[Serializable()]
[XmlRoot("composite", Namespace="", IsNullable=false)]
public partial class CT_Composite 
{
    [XmlElement("component", typeof(CT_Component))]
    [XmlElement("composite", typeof(CT_Composite))]
    public object[] Items { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}

我从下面的 xsd 创建了那些,我总是从 xsd 到生成的类,因为我永远无法获得正确的属性装饰。它的要点是递归 CT_Composite 类型:

<xs:element name="component" type="CT_Component" />
<xs:element name="composite" type="CT_Composite" />
<xs:complexType name="CT_Component">
  <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="CT_Composite" >
  <xs:choice minOccurs="1" maxOccurs="unbounded">
    <xs:element ref="component" />
    <xs:element name="composite" type="CT_Composite" />
  </xs:choice>
  <xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>

序列化代码是一样的。变量声明:

var composite = new CT_Composite() {
            Name = "root",
            Items = new object[] {
                new CT_Composite() {
                    Name = "child1",
                    Items = new object[] {
                        new CT_Component() {Name="child2"},
                        new CT_Component() {Name="child3"}
                    } } } };

如果你对模式更加正统,你可以使用:

[Serializable()]
[XmlRoot("component", Namespace="", IsNullable=false)]
public class Component {
    [XmlAttribute("name")] public string Name { get; set;}
}

[Serializable()]
[XmlRoot("composite", Namespace="", IsNullable=false)]
public class Composite : Component {
    [XmlElement("component", typeof(Component))]
    [XmlElement("composite", typeof(Composite))]
    public object[] Items { get; set; }
}

关于c# - ISerializable 与递归 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668133/

相关文章:

c# - 使用 DDD 创建子实体的正确方法

c# - 在异步 Controller 中使用同步操作

javascript - JS数组中 "|"(管道)符号的用途是什么

wordpress - 如何在我的缩略图上放置文本和阴影 (php)?

python - 使用基于列表列表中的项目的键创建 Python 字典

c# - 如何修复验证以使其更好?

c# - 如何模拟 BitConverter.IsLittleEndian 与我的单元测试相反的环境?

php - 用数组附加 protected 对象

Javascript 在创建过程中清理数组

c# - 怎样做才能只得到一个列表?