Inheritance XmlSerializer C#

本文关键字:XmlSerializer Inheritance | 更新日期: 2023-09-27 18:01:48

我有这三个类:

Class Image : Asset
Class Sound : Asset
Class Video : Asset

所有东西都序列化了但是当我创建这个项目时:

Class Master
List<Asset> assets //property

该类的一个实例,例如:

Image i = new Image();
Sound s = new Sound();
Video v = new Video();
Master m =  new Master( new List<Asset>{i,s,v} )

异常不序列化" invalidoperationexception -生成XML文档出错"在innerException中:{"类型是MyApplication。视频是意料之外的。使用XmlInclude或SoapInclude属性来指定静态未知的类型。"}

. .任何想法?

Inheritance XmlSerializer C#

Asset类上添加XmlInclude属性:

[XmlInclude(typeof(Video))]
[XmlInclude(typeof(Sound))]
[XmlInclude(typeof(Image))]
public class Asset
{
    ...
}