为此对象添加KnownType属性(Attribute)。类型为用接口声明的属性(Property)的所有可能类型。
示例如下:
1 public interface IKey 2 { 3 [DataMember] 4 int Id { get; set; } 5 6 [DataMember] 7 string Name { get; set; } 8 } 9 10 [DataContract] 11 public abstract class KeyBase : IKey 12 { 13 [DataMember] 14 public int Id { get; set; } 15 16 [DataMember] 17 public string Name { get; set; } 18 } 19 20 [DataContract] 21 public class Key : KeyBase 22 { 23 [DataMember] 24 public int Age { get; set; } 25 } 26 27 [KnownType(typeof(KeyBase))] 28 [KnownType(typeof(Key))] 29 [DataContract] 30 public class Data 31 { 32 [DataMember] 33 public IKey Key { get; set; } 34 35 [DataMember] 36 public string Message { get; set; } 37 }
时间: 2024-10-10 16:40:26