Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 财产';在XML反序列化时,将丢弃s派生类(即用基类替换)_C#_Xml_Serialization_Deserialization_Xml Deserialization - Fatal编程技术网

C# 财产';在XML反序列化时,将丢弃s派生类(即用基类替换)

C# 财产';在XML反序列化时,将丢弃s派生类(即用基类替换),c#,xml,serialization,deserialization,xml-deserialization,C#,Xml,Serialization,Deserialization,Xml Deserialization,我有一个游戏数据类,如下所示: public class SaveGameData { public virtual List<PropertyContainer> properties {get; set; } } 我正在尝试在播放会话之间保存/加载这些数据,我在这个过程中使用XML序列化/反序列化 问题在于,在PropertyContainer类中,派生属性有时用于property类的子分区,如下所示: PropertyContainer container

我有一个游戏数据类,如下所示:

public class SaveGameData  {

    public virtual List<PropertyContainer> properties {get; set; }

}  
我正在尝试在播放会话之间保存/加载这些数据,我在这个过程中使用XML序列化/反序列化

问题在于,在PropertyContainer类中,派生属性有时用于property类的子分区,如下所示:

PropertyContainer container = new PropertyContainer();
container.property = derivedProperty; 
<?xml version="1.0" encoding="utf-16"?>
<SaveGameData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <properties>

    <PropertyContainer>

      <property xsi:type="DerivedProperty">

      <BasePropertyData>1</BasePropertyData>
      <DerivedPropertyData>1</DerivedPropertyData>

      </property>

    </PropertyContainer>

  </properties>

</SaveGameData>  
当容器被序列化时,派生类及其特殊属性也会被保存,这里没有问题

以下是序列化代码:

serializer = new System.Xml.Serialization.XmlSerializer(typeof(SaveGameData));

SaveGameData dataToSave = GetSaveGameData();
using (var stream = new StringWriter()) {
serializer.Serialize(stream, dataToSave); 
...write to file... 

 }
序列化过程似乎正在工作,因为派生类被正确识别并保存到XML文件中,XML输出如下所示:

PropertyContainer container = new PropertyContainer();
container.property = derivedProperty; 
<?xml version="1.0" encoding="utf-16"?>
<SaveGameData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <properties>

    <PropertyContainer>

      <property xsi:type="DerivedProperty">

      <BasePropertyData>1</BasePropertyData>
      <DerivedPropertyData>1</DerivedPropertyData>

      </property>

    </PropertyContainer>

  </properties>

</SaveGameData>  
这意味着加载XML数据后,对派生属性调用
GetType()
(例如
saveGameData.properties[0].GetType()
,假设该属性是DerivedProperty)将生成基类,即
属性
;通过扩展,它将丢弃所有DerivedProperty的属性。这就是问题所在

附言: 我尝试添加
xmlclude
属性,但没有任何更改:

[System.Xml.Serialization.XmlInclude(typeof(DerivedProperty))]
public class Property {

...

}  

我如何解决这个问题?是否有一种可行的方法替代我的方法?

一种选择是切换到。然后使用来识别子类

public class PropertyContainer {

    public Property property {get; set; }//Could be set to DerivedProperty

}
[KnownType(typeof(DerivedProperty))]
public class Property {
    public int BasePropertyData {get; set;}
}
public class DerivedProperty : Property {
    public int DerivedPropertyData {get; set; }    
}

[xmlclude(typeof(DerivedProperty))]
装饰
属性
类应该是可行的。实际上,如果没有它,
XmlSerializer
甚至不允许您首先序列化
DerivedProperty

以下程序是基于您的代码的工作示例。请确认您的机器上的控制台输出也是
True
。如果是,则需要确定代码与原始尝试之间的差异。然后更新您的问题,以便我们可以更深入地挖掘

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        string savedGame = @"
<SaveGameData xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <properties>
    <PropertyContainer>
      <property xsi:type='DerivedProperty'>
        <BasePropertyData>1</BasePropertyData>
        <DerivedPropertyData>1</DerivedPropertyData>
      </property>
    </PropertyContainer>
  </properties>
</SaveGameData>";
        XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
        SaveGameData result;

        using (StringReader stream = new StringReader(savedGame))
        {
            result = (SaveGameData)serializer.Deserialize(stream);
        }

        Console.WriteLine(result.properties[0].property is DerivedProperty); // True
    }
}

public class SaveGameData
{
    public virtual List<PropertyContainer> properties { get; set; }
}

public class PropertyContainer
{
    public Property property { get; set; }//Could be set to DerivedProperty
}

[XmlInclude(typeof(DerivedProperty))]
public class Property
{
    public int BasePropertyData { get; set; }
}

public class DerivedProperty : Property
{
    public int DerivedPropertyData { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml.Serialization;
班级计划
{
静态void Main()
{
字符串savedGame=@”
1.
1.
";
XmlSerializer serializer=新的XmlSerializer(typeof(SaveGameData));
保存游戏数据结果;
使用(StringReader流=新StringReader(SavedName))
{
结果=(SaveGameData)序列化程序。反序列化(流);
}
Console.WriteLine(result.properties[0]。属性为DerivedProperty);//True
}
}
公共类SaveGameData
{
公共虚拟列表属性{get;set;}
}
公共类属性容器
{
公共属性属性{get;set;}//可以设置为DerivedProperty
}
[xmlclude(typeof(DerivedProperty))]
公共类财产
{
public int BasePropertyData{get;set;}
}
公共类派生属性:属性
{
public int-DerivedPropertyData{get;set;}
}

我不完全确定这个不存在的bug是如何通过我的,但它肯定是在浪费我的时间。谢谢你指出这一点。