C# WCF and Object Inheritence

Posted by Michael Edwards on Stack Overflow See other posts from Stack Overflow or by Michael Edwards
Published on 2011-01-05T12:26:33Z Indexed on 2011/01/05 12:53 UTC
Read the original article Hit count: 188

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf