0

通常はシリアル化されているクラスを逆シリアル化しようとしています。

public class MyClass
{
   private List<Section> sections = new List<Section>();
   public List<Section> Sections
   {
        get
        {
          return this.sections;
        }
   }
}

public class Section1: Section
{
    public string MyProperty {get;set;}
}

public class Section2 : Section
{
    public string MyProperty2 {get;set;}
}

クラスMyClassをエラーなしでシリアル化しましたが、逆シリアル化しようとすると、セクションに空のプロパティを持つクラスMyClassが表示されました(このプロパティは空でした)。

これはなぜですか、この問題を解決する方法は?

xmlの例:

<MyClass>
  <Sections>
     <Section1> 
       <MyProperty>foo1</MyProperty>
     </Section1>
     <Section1> 
       <MyProperty>foo2</MyProperty>
     </Section1>
     <Section2> 
       <MyProperty2>boo1</MyProperty2>
     </Section2>
  </Sections>
</MyClass>

コードのシリアル化と逆シリアル化:

シリアル化/逆シリアル化に使用されたクラス:

public class ObjectSerializer
{
    private readonly XmlAttributeOverrides xmlAttributeOverrides = new XmlAttributeOverrides();

    public void XmlSerialize<T>(T value, TextWriter outStream)
    {
        Type type = typeof (T);
        object[] result = type.GetCustomAttributes(typeof (SerializableAttribute), true);
        if (result != null)
        {
            var serializer = new XmlSerializer(type, this.xmlAttributeOverrides);
            serializer.Serialize(outStream, value);
        }
    }

    public T XmlDeserialize<T>(string xml)
    {
        var textReader = new XmlTextReader(new StringReader(xml));
        var xmlSerializer = new XmlSerializer(typeof(T));

        var result = xmlSerializer.Deserialize(textReader);
        return (T)result;
    }


    public void ExportOverridesFrom<TAssemply, TBaseType, TObject>(
        Expression<Func<TObject, object>> propertySelector)
    {
        IEnumerable<Type> inheritedTypes = typeof (TAssemply).Assembly.GetTypes().Where(t => t.BaseType == typeof (TBaseType));
        var xmlAttributes = new XmlAttributes();
        foreach (Type type in inheritedTypes)
        {
            var xmlElementAttribute = new XmlElementAttribute {Type = type};
            xmlAttributes.XmlElements.Add(xmlElementAttribute);
        }
        PropertyInfo objectProperty = Reflect<TObject>.GetProperty(propertySelector);
        this.xmlAttributeOverrides.Add(typeof (TObject), objectProperty.Name, xmlAttributes);
    }
}

シリアル化:すべて良いです!

var objectSerializer = new ObjectSerializer();
objectSerializer.ExportOverridesFrom<Section1, Section, MyClass>(p => p.Sections);
objectSerializer.XmlSerialize(myClass, resultStream);

デシリアリザトイン:すべてが悪い!

 xml - result serialization.
 var result = objectSerializer.XmlDeserialize<MyClass>(xml);

ありがとう、オクサナ

4

4 に答える 4

0

プライベートセクションメンバーの宣言を次のように変更する必要があります。

private List<Section> sections = new List<Section>();

それ以外の場合はnullになり、に割り当てることができません。

さらに、SectionsプロパティにはGetterしかありません。setterが必要です。そうでない場合、設定されることはありません。

public List<Section> Sections
{
    get
    {
      return this.sections;
    }

    set
    {
      this.sections = value;
    }

}
于 2009-12-10T19:58:27.807 に答える
0

List<Section> sectionsを使用する前に、を空のリストにインスタンス化する必要があります。

于 2009-12-10T19:54:33.213 に答える
0
[DataContract]
[KnownType(typeof(Section1))]
[KnownType(typeof(Section2))]
public class Section
{ 

} 

DataContract シリアライザーを使用してみてください。派生型を渡すことができます

シリアル化:

DataContractSerializer ser = new DataContractSerializer(typeof(Section),new Type[] { typeof(Section1),typeof(Section2)});
     ser.WriteObject(writer, sectionObj);
 writer.Close();

デシリアライズ:

DataContractSerializer deser = new DataContractSerializer(typeof(Section),new Type[] { typeof(Section1),typeof(Section2)}););            
Section deserialisedSection = (Section)deser.ReadObject(reader, true);
于 2010-03-23T14:33:33.500 に答える
-1

この問題を解決する必要があります。

シリアライザーを作成するときは、コンストラクターで xmlAttributeOverrides を渡します。(つまり、シリアライゼーションと同じ)。

 public T XmlDeserialize<T>(string xml)
    {
        var textReader = new XmlTextReader(new StringReader(xml));
        var xmlSerializer = new XmlSerializer(typeof(T), xmlAttributeOverrides); <--this

        var result = xmlSerializer.Deserialize(textReader);
        return (T)result;
    }

それはうまくいきます!

于 2009-12-11T15:55:26.497 に答える