C# WCF and Object Inheritence
- by Michael Edwards
I have the following setup of two classes:
[SerializableAttribute]
public class ParentData{
[DataMember]
public string Title{get;set;}
}
[DataContract]
public class ChildData : ParentData{
[DataMember]
public string Abstract{get;set;}
}
These two classes are served through a WCF service. However I only want the service to expose the ChildData class to the end user but pull the marked up DataMember properties from the parent. E.g. The consuming client would have a stub class that looked like:
public class ChildData{
public string Title{get;set;}
public string Abstract{get;set;}
}
If I uses the parent and child classes as above the stub class only contains the Abstract property.
I have looked at using the KnownType attribute on the ChildData class like so:
[DataContract]
[KnownType(typeOf(ParentData)]
public class ChildData : ParentData{
[DataMember]
public string Abstract{get;set;}
}
However this didn't work.
I then applied the DataContract attribute to the ParentData class, however this then creates two stub classes in the client application which I don't want.
Is there any way to tell the serializer that it should flatten the inheritance to that of the sub-class i.e. ChildData