4

同様の型付けされた子のリストを含む 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 回、次々に呼び出されます。

ネストされた配列として子をシリアライザーに追加すると、デシリアライズ時に正しいサイズの配列が返されますが、すべての要素が null になります。

ただし、ネストされた配列からネストされたリストにタイプを変更すると、子のコンストラクターが呼び出された後に null 要素が魔法のように修正されます。

私が知りたいのは:

  1. ネストされたリストの特別な点は何ですか?
  2. このような再帰構造を持つクラスをシリアライズするための推奨される方法は何ですか?

アップデート:

逆シリアル化が行われた後に呼び出される追加のインターフェイス、IDeserializationCallback.OnDeserialization があるようです (呼び出し順序は非決定論的です)。逆シリアル化された配列をコンストラクターの一時メンバー変数に格納し、このメソッドでリストに割り当てることができます。何かが欠けていない限り、実装を一時変数で混乱させる必要があるため、これは理想的とは言えません。

4

1 に答える 1

0

複合パターンで行きます。以下のソリューションは、代わりにそれを使用する場合BinaryFormatter、(メインのように) とアプローチの両方を解決します。クラスを置き換えます。XmlSerializerCompositeComponentNested

[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; }
}
于 2015-11-16T18:29:07.070 に答える