ASMX Web服务未序列化抽象基类 [英] ASMX Web service not serializing abstract base class

查看:81
本文介绍了ASMX Web服务未序列化抽象基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类.让我们将其称为生命形式".看起来像这样:

I have an abstract class. Let's call it Lifeform. It looks something like:

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

(虚拟属性是由于我使用的是nHibernate,如果它们不是虚拟属性,则会发出抱怨.)

(The virtual attribute is due to the fact that I'm using nHibernate, which whines if they're not virtual properties.)

然后我有了一个继承自该Lifeform类的类;我们称之为人类.看起来像这样:

I then have a class which inherits from that Lifeform class; we'll call it Human. It looks something like:

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

一切都很可爱,我可以使用我的课程,在使用Human时,Human会获得Legs,Arms和Alive属性.例外,就是当我尝试使用Human类制作Web服务时.序列化的对象为我提供了帽子",年龄"和名称"-但是没有腿,手臂或活着"属性.

Everything's lovely, I can use my classes, Human gets the Legs, Arms, and Alive properties when I'm using it. Except, that is, when I attempt to make a web service using the Human class. The serialized object gives me Hat, Age, and Name - but no Legs, Arms, or Alive properties.

我已经看到一种解决方法,建议使用

I've seen a workaround that suggests using

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

在基类(Lifeform)上,但这似乎是违反OO的可怕骇客.在基类上放链接到继承它的类?哇!

On the base class (Lifeform), but that seems like a horrible hack that violates OO. Putting links on the base class to the classes that inherit it? Eww.

以前有人遇到过此特定问题吗?有什么想法吗?如果有更深入的示例可以帮助描述我在做什么,我将提供更多代码.

Has anyone run into this specific issue before? Have any ideas? I'll provide more code if a more in-depth example would help describe what I'm doing more.

推荐答案

根据我所读的内容,可以在返回对象的Web方法(而不是基类)中包含XMLInclude属性.仍然不是很漂亮,但是比将派生类名称放在基类中可能更吸引您.我还没有尝试过,但是我认为您可以做这样的事情.

From what I've read, you can include the XMLInclude attribute on the web method returning the object rather than in the base class. Still not pretty, but might appeal to you a little more than putting derived class names in the base class. I haven't tried it out, but I think you can do something like this.

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

这篇关于ASMX Web服务未序列化抽象基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆