Jac*_*son 9 c# web-services xml-serialization asmx
我有一个抽象的课.我们称之为Lifeform.它看起来像:
public abstract class Lifeform {
public virtual int Legs { get; set; }
public virtual int Arms { get; set; }
public virtual bool Alive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
(虚拟属性是由于我正在使用nHibernate这一事实,如果它们不是虚拟属性,它会发出呜呜声.)
然后我有一个继承自Lifeform类的类; 我们称之为人类.它看起来像:
public class Human: Lifeform {
public virtual bool Hat { get; set; }
public virtual int Age { get; set; }
public virtual string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一切都很可爱,我可以使用我的课程,当我使用它时,人类会获得腿部,手臂和活力属性.除此之外,也就是说,当我尝试使用Human类创建Web服务时.序列化对象为我提供了帽子,年龄和名字 - 但没有腿,武器或活动属性.
我见过一个建议使用的解决方法
[System.Xml.Serialization.XmlInclude(typeof(Human))]
Run Code Online (Sandbox Code Playgroud)
在基类(Lifeform)上,但这似乎是一个违反OO的可怕黑客.将基类上的链接放到继承它的类上?好恶.
有没有人遇到过这个具体问题?有什么想法吗?如果更深入的示例有助于描述我正在做的更多,我将提供更多代码.
从我读过的内容中,您可以在返回对象而不是基类的Web方法中包含XMLInclude属性.仍然不漂亮,但可能比在基类中放置派生类名更吸引你.我没有试过,但我认为你可以做这样的事情.
[WebMethod]
[XmlInclude(typeof(LifeForm))]
public Human GetHuman()
{
return new Human();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4091 次 |
| 最近记录: |