In WCF, can data contract classes inherit from one another?

 Posted by Lokeshkumarn on 5/31/2012 | Category: WCF Interview questions | Views: 9320 | Points: 40
Answer:

[DataContract]
public class A
{
[DataMember]
public MyCustomType AValue1{ get; set; }

[DataMember]
public MyCustomType AValue2 { get; set; }
}

[DataContract]
public class B: A
{
[DataMember]
public double BValue1{ get; set; }

[DataMember]
public double BValue2 { get; set; }
}

the metadata fails to load when testing the services.
What needs to be done to load both super and sub class.
We must add KnownType Attribute.

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

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


Source: http://stackoverflow.com/quest | Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Akiii on: 6/2/2012 | Points: 10
Hi Lokesh,
without using the [KnownType(typeof()] attribute I can do that by creating object of B that is the derived class !
And its working fine.



Thanks and Regards
Akiii

Login to post response

More Interview Questions by Lokeshkumarn