ASMX Web服务不序列化抽象基类

我有一个抽象类。 我们称之为Lifeform。 它看起来像:

public abstract class Lifeform { public virtual int Legs { get; set; } public virtual int Arms { get; set; } public virtual bool Alive { get; set; } } 

(虚拟属性是由于我正在使用nHibernate这一事实,如果它们不是虚拟属性,它会发出呜呜声。)

然后我有一个inheritance自Lifeform类的类; 我们称之为人类。 它看起来像:

 public class Human: Lifeform { public virtual bool Hat { get; set; } public virtual int Age { get; set; } public virtual string Name { get; set; } } 

一切都很可爱,我可以使用我的课程,当我使用它时,人类会获得腿部,arm和活力属性。 除此之外,也就是说,当我尝试使用Human类创建Web服务时。 序列化对象给了我帽子,年龄和名字 – 但没有腿,武器或活着的属性。

我见过一个建议使用的解决方法

 [System.Xml.Serialization.XmlInclude(typeof(Human))] 

在基类(Lifeform)上,但这似乎是一个违反OO的可怕黑客。 将基类上的链接放到inheritance它的类上? 好恶。

有没有人遇到过这个具体问题? 有什么想法吗? 如果更深入的示例有助于描述我正在做的更多,我将提供更多代码。

根据我的阅读,您可以在Web方法上包含XMLInclude属性,该方法返回对象而不是基类。 仍然不漂亮,但可能比在基类中放置派生类名更吸引你。 我没试过,但我认为你可以这样做。

 [WebMethod] [XmlInclude(typeof(LifeForm))] public Human GetHuman() { return new Human(); } 

与VB.NET完全相同的问题。 使用XMLInclude属性,虽然丑陋,但做到了。