返回 DataContract 的子类

发布于 2024-12-13 11:31:05 字数 743 浏览 2 评论 0原文

我有一个 DataContract,用作 WCF 服务的返回类型。

[DataContract]
public NameResult
{
    [DataMember]
    public string Name { get; set; }
}

但是,我想在服务端存储附加信息,因此我创建了一个子类:

internal ServiceNameResult : NameResult
{
    internal Guid ID { get; set; }
}

但是,我似乎无法使用此实例作为结果值(我在客户端上收到的错误不是很有帮助 - 无法识别)错误 109 (0x6d)。

基本上,如果我这样做了;

NameResult GetName() 
{
    NameResult result = {...}
    return result;
}

那么它就可以了,但如果我这样做了

NameResult GetName()
{
    ServiceNameResult result = {...}
    return result;
}

;我真的不想将属性从 ServiceNameResult 复制到新的 NameResult。去的路使这个工作?

我已经将 [IgnoreDataMember] 放在子类上,但这没有什么区别,

谢谢。

I have a DataContract which I use as a return type from a WCF service.

[DataContract]
public NameResult
{
    [DataMember]
    public string Name { get; set; }
}

However, I want to store additional information on the service side, so I create a subclass:

internal ServiceNameResult : NameResult
{
    internal Guid ID { get; set; }
}

However, it seems I am unable to use instances of this as a result value (the error I get on the client isn't very helpful - Unrecognized error 109 (0x6d).

Basically, if I do;

NameResult GetName() 
{
    NameResult result = {...}
    return result;
}

Then it works, but if I do;

NameResult GetName()
{
    ServiceNameResult result = {...}
    return result;
}

It doesn't. I don't really want to have to copy the properties from the ServiceNameResult to a new NameResult. Hopefully there is a way to make this work?

I've already put [IgnoreDataMember] on the subclass, but that makes no difference.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

世界等同你 2024-12-20 11:31:06

这是解决这个问题的一种方法。您可以使用组合来实现您正在寻找的内容:

internal class ServiceNameResult
{
    object OtherInformation { get; set; }

    NameResult Result { get; set; }
}

因此您的内部服务实现可以保存对客户端返回对象的引用以及其他信息,但不会污染您的接口。

Here is one way to approach this problem. You could use composition to achieve what you're looking for:

internal class ServiceNameResult
{
    object OtherInformation { get; set; }

    NameResult Result { get; set; }
}

So your internal service implementation can hold a reference to the client return object as well as additional information, but you don't pollute your interface.

£噩梦荏苒 2024-12-20 11:31:06

您需要将 ServiceNameResult 定义为已知类型

You'll need to define ServiceNameResult as a known type

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