首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有递归子的ISerializable

具有递归子的ISerializable
EN

Stack Overflow用户
提问于 2015-11-12 09:27:44
回答 1查看 943关注 0票数 4

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

代码语言:javascript
复制
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和嵌套的序列化构造函数被一个接一个地调用4次。

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

但是,将类型从嵌套数组更改为嵌套列表将神奇地在调用子构造函数之后修复空元素。

我想知道的是:

  1. 嵌套列表有什么特别之处?
  2. 用这样的递归结构序列化类的推荐方法是什么?

更新:

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

EN

回答 1

Stack Overflow用户

发布于 2015-11-16 18:29:07

我会采用复合模式。下面的解决方案解决了BinaryFormatter (如您的主)和XmlSerializer方法,如果您要使用这个方法的话。CompositeComponent替换Nested类。

代码语言:javascript
复制
[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类型:

代码语言:javascript
复制
<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>

序列化代码是相同的。变量声明:

代码语言:javascript
复制
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"}
                    } } } };

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

代码语言:javascript
复制
[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; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33668133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档