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

发布于 2024-07-30 07:22:16 字数 881 浏览 2 评论 0原文

我有一个抽象类。 我们称之为生命体。 它看起来像这样:(

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

虚拟属性是因为我使用的是 nHibernate,如果它们不是虚拟属性,它会发出呜呜声。)

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

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

一切都很可爱,我可以使用我的课程,当我使用它时,人类会获得腿部、手臂和活动属性。 除非我尝试使用 Human 类创建 Web 服务。 序列化对象为我提供了帽子、年龄和姓名,但没有腿、手臂或活着的属性。

我见过一个建议使用

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

On 基类 (Lifeform) 的解决方法,但这似乎是一个违反 OO 的可怕黑客。 在基类上放置指向继承它的类的链接? 呃。

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

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; }
}

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

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; }
}

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))]

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-08-06 07:22:16

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

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

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();
}
无语# 2024-08-06 07:22:16

在 VB.NET 中遇到了完全相同的问题。 使用 XMLInclude 属性虽然丑陋,但却达到了目的。

Ran into the exact same problem with VB.NET. Using the XMLInclude attribute, while ugly, did the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文