[KnownType]
is needed to tell it about subtypes. The disadvantage of not using it is that the following won't work:
[DataContract]
class Foo {}
[DataContract]
class Bar : Foo {}
with a method on the WCF interface that returns:
public Foo GetFoo() { return new Bar(); }
Without the attribute, the serializer (especially for mex/proxy-generated types) won't know about Bar
, and it will fail. With the attribute:
[DataContract, KnownType(typeof(Bar))]
class Foo {}
it will work. This only applies to DataContractSerializer
- with NetDataContractSerializer
you get the type data in a different way.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…