0

This might sound like a repeated question from other topics, but it defers in the Interface definition.

I have an Interface, lets say named IClass1, which contains in its definition a list of interfaces, say IClass2

I have a business unit to implement this interface; implementation naturally will be like this

class Class1: IClass1
{
    ....whatever properties
    List<IClass2> DataItems { get; set; }
}

class Class2:IClass2
{
    ...whatever properties
}

The problem is that, XmlSerializer will complain about the list in Class1, because it is an interface! So my question is: Simply, I always know what is the type of the instances that should be de-serialized in DataItems list, which will be of type Class2, how to tell my XML serializer that?

I've saw a lot of workarounds, using a dummy properties and ignore the list of interfaces during the serializing/de-serializing process is the most suitable one I think, and I i certainly don't want to invent my own serializer or re implement serialization in my classes

4

1 回答 1

1

如果您正在使用 DataContracts,则可以使用KnownTypeAttribute为反序列化器提供一个或多个类型的列表,该列表在反序列化时应使用。

采用字符串参数的版本允许您指定将返回一系列类型的方法的名称,这在某些情况下可能更易于使用。

我提供的链接在页面末尾有一些示例代码。

于 2013-05-21T07:40:31.250 回答