为此对象添加KnownType属性(Attribute)。类型为用接口声明的属性(Property)的所有可能类型。
示例如下:
public interface IKey
{
[DataMember]
int Id { get; set; } [DataMember]
string Name { get; set; }
} [DataContract]
public abstract class KeyBase : IKey
{
[DataMember]
public int Id { get; set; } [DataMember]
public string Name { get; set; }
} [DataContract]
public class Key : KeyBase
{
[DataMember]
public int Age { get; set; }
} [KnownType(typeof(KeyBase))]
[KnownType(typeof(Key))]
[DataContract]
public class Data
{
[DataMember]
public IKey Key { get; set; } [DataMember]
public string Message { get; set; }
}