开发者

Can data contract classes inherit from one another?

I want to use data contract class as base class of some other classes. I know that if i want to define inherit between two classes i need to use 'KnownType' attribute.

But in case i want to make the inherit between more then two classes .. lets say i开发者_高级运维 have also class C that inherit from class A - how i can do it ?

I try to add ' [KnownType(typeof(C))] ' to class A definition - buts it is not working.

[DataContract]
[KnownType(typeof(B))]
public class A
{
   [DataMember]
   public string Value { get; set; }
}

[DataContract]
public class B : A
{
   [DataMember]
   public string OtherValue { get; set; }
}

[DataContract]
public class C : A
{
   [DataMember]
   public string OtherValue { get; set; }
}


[DataContract]
[KnownType(typeof(B))]
[KnownType(typeof(C))]
public class A
{
 ...
}

 ...


I would have thought you'd want ;

[KnownType(typeof(A))]
public class B : A
{
...
}

as B "is a" A (for all intents and purposes of WCF serialisation). However, as you've currently got it, you are saying that A "is a" B, which isn't always the case (ie, typeof C) but on further investigation....I'd be wrong.

I've come across the Person : Contact example a few times, and I just thought I'd search for that to make sure my understanding was right, and this link shows the syntax the same way you have it (anotations on the base class). It shows stacking in the following way;

   1. [DataContract]
   2. [KnownType(typeof(Customer))]
   3. [KnownType(typeof(Person))]
   4. class Contact {...}
   5.  
   6. [DataContract]
   7. class Person : Contact {...}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜