我有这个对象结构:
[DataContract]
[KnownType(typeof(ChildClassA))]
[KnownType(typeof(ChildClassB))]
public class MainClass
{
[DataMember]
public string PropA { get; set; }
[DataMember]
public IList<IMyInterface> GenericInterfaceList { get; set; }
//....
}
public interface IMyInterface
{
int Id { get; set; }
//....
}
[DataContract]
public abstract class BaseClass : IMyInterface
{
[DataMember]
public int Id { get; set; }
//....
}
[DataContract]
public class ChildClassA : BaseClass
{
//....
}
[DataContract]
public class ChildClassB : BaseClass
{
//.....
}
我希望它在没有我的通用接口列表的DataContractSerializer
情况下发出 Xml :a:anyType i:type=
<MainClass xmlns="MYNAMESPACE" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PropA> dfgdsfg gsd</PropA>
<GenericInterfaceList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<ChildClassA>
<Id>1</Id>
</ChildClassA>
<ChildClassB>
<Id>2</Id>
</ChildClassB>
</GenericInterfaceList>
</MainClass>
代替:
<MainClass xmlns="MYNAMESPACE" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<PropA> dfgdsfg gsd</PropA>
<GenericInterfaceList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:anyType i:type="ChildClassA">
<Id>1</Id>
</a:anyType>
<a:anyType i:type="ChildClassB">
<Id>2</Id>
</a:anyType>
</GenericInterfaceList>
</MainClass>
我尝试了自定义DataContractResolver
,但它不是正确的工具。找不到缩小该 Xml 的方法。