Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
.net 反序列化没有显式XmlArrayItem属性的继承类型_.net_Xml Serialization - Fatal编程技术网

.net 反序列化没有显式XmlArrayItem属性的继承类型

.net 反序列化没有显式XmlArrayItem属性的继承类型,.net,xml-serialization,.net,Xml Serialization,我有一个如下所示的XML文档: <MyEntity> <Vehicles> <Car /> <Truck /> </Vehicles> </MyEntity> 是否可以在不必为每种可能的车辆类型使用XmlArrayItem属性修饰MyEntity.Vehicles属性的情况下执行此操作?我可能会在编译时得到我不知道的新类型的车辆,但在运行时可以发现。除了添加属性之外,我唯一能

我有一个如下所示的XML文档:

<MyEntity>
    <Vehicles>
        <Car />
        <Truck />
    </Vehicles>
</MyEntity>

是否可以在不必为每种可能的车辆类型使用XmlArrayItem属性修饰MyEntity.Vehicles属性的情况下执行此操作?我可能会在编译时得到我不知道的新类型的车辆,但在运行时可以发现。

除了添加属性之外,我唯一能想到的方法是在MyEntity上实现IXmlSerializer并手动处理它。当然,您还需要为不同的车辆类型注册处理程序。

除了添加属性之外,我能想到的唯一方法是在MyEntity上实现IXmlSerializer并手动处理它。当然,您还需要为不同的车辆类型注册处理程序

public class MyEntity
{
    public Vehicle[] Vehicles { get; set; }
}

public class Vehicle {}
public class Car : Vehicle {}
public class Truck : Vehicle {}