Reputation: 9926
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; }
}
Upvotes: 0
Views: 1508
Reputation: 6344
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 {...}
Upvotes: 1
Reputation: 2468
[DataContract]
[KnownType(typeof(B))]
[KnownType(typeof(C))]
public class A
{
...
}
...
Upvotes: 2