如何通过WCF传递派生类的字段? [英] How to pass a Derived Class's fields over WCF?

查看:98
本文介绍了如何通过WCF传递派生类的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我派生了类"bar",该类具有附加的基类字段. WCF服务必须接受基类类型(因为必须使用同一服务的多个派生类,每个派生类将具有其他字段).我需要通过WCF传递派生类的字段,下面是一个示例.

I have derived class "bar" that has additional fields to the base class. The WCF service must accept the base class type (because there are several derived classes that have to use the same service, each derived class will have additional fields). I need to transfer the derived class''s fields over WCF, below is an example.

[KnownType(typeof(bar))]
[DataContract]
public class fooBase
{
[DataMember]
    public string a;
}

[DataContract]
public class bar : fooBase
{
    [DataMember]
    public string b;
}



我的WCF服务接受fooBase.当客户端将bar实例传递给服务时,将使用正确的数据填充a,但是b始终为null.

WCF是否可以转移派生类的字段?如果是这样,怎么办?如果没有,那么使用Remoting可以实现吗?

谢谢!



My WCF service accepts fooBase. When the client passes an instance of bar to the service, a is populated with the correct data, but b is always null.

Is it possible for WCF to transfer the fields of derived classes? If so, how? If not, is this possible with Remoting?

Thanks!

推荐答案

本文可能会有所帮助:数据协定继承-已知类型和通用解析器 [ ^ ]
This article may help: Data Contract Inheritance - Known Types and the Generic Resolver[^]


一切正确.我认为,您从未为该字段分配任何值,仅此而已.手动将值写入XML文件,然后使用数据协定序列化程序读取该值,以查看其出现.

一个小建议:始终在数据协定的根对象上使用名称空间:如果名称空间当然是基于世界唯一的URL,那么它将使您的数据模型元数据(和数据格式)成为世界唯一的.

—SA
Everything is correct. I think, you just never assigned a value to this field, that''s it. Manually write the value in XML file and read it with the data contract serializer to see that it appears.

One little recommendation: always use the namespace on the root object of the data contract: it will make your data model meta-data (and data format) world-unique, if the namespace is based on world-unique URL of course.

—SA


这篇关于如何通过WCF传递派生类的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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