使用XmlMessageFormatter序列化基础对象类型列表

时间:2013-07-04 09:09:13

标签: xml-serialization msmq message-queue

我有一个像这样的对象:

public class Intent
{
    public List<Entity> Updates { get; set; }
}

我希望将其序列化为XML,以便使用MSMQ作为消息传递。 Entity类型列表可以包含从Entity继承的任意数量的类实例。例如,可能有:

public Person : Entity { /* ... */ }
public Vehicle : Entity { /* ... */ }

我正在使用XmlMessageFormatter,到目前为止我已定义为:

XmlMessageFormatter _formatter =
        new XmlMessageFormatter(new[] { typeof(T) });

此实例中的T为Intent(如上所述)。

麻烦的是,当代码实际上尝试序列化以下异常时:

The type CoreApi.Domain.Person was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

我相信这是因为我需要以某种方式告诉序列化程序Person是实体的子类。

我见过的解决方案基本上需要用多个XmlInclude装饰来装饰Entity,这在我的场景中是行不通的,因为Entity的继承者列表很大并且可能会增长 - 我不想不断地将此列表更新为新的继承者添加。

我见过其他使用XmlSerializer的解决方案,传入已知类型的列表,这样做的问题是我不知何故需要用不兼容的XmlSerialiser实例替换XmlMessageSerialiser。

0 个答案:

没有答案