WCF序列化对象继承? [英] WCF Serialization with object inheritance?

查看:151
本文介绍了WCF序列化对象继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个对象,一个是我们的企业级,另一个在我们的服务水平。服务对象是从企业继承。下面是一个简单的例子:

I have two objects, one is in our enterprise level and another in in our service level. The service object is inheriting from the enterprise. Here is a quick example:

[DataContract]
public class EnterpriseObject{
     [DataMember]
     int ID{get; set;}

     string InternalUse{get; set;}
}

[DataContract]
public class ServiceObject: EnterpriseBaseObject{
     [DataMember]     
     string Address{get; set;}
}

是否有可能只公开ServiceObject(从EnterpriseObject继承属性)的序列化?我不想让客户看到作为一个选项列出的企业对象?正如你所看到的例子将DataMember属性未设置​​为InternalUser财产。这是唯一的办法做到这一点?谢谢

Is it possible to only expose the ServiceObject (with inherited properties from EnterpriseObject) in the serialization? I do not want the client to see the enterprise object listed as an option? As you can see in the example the DataMember attribute is not set for the InternalUser property. Is that the only way to do it? Thanks

推荐答案

您通过添加处理继承[KnownType(typeof运算(ServiceObject))] EnterpriseBaseObject - 然而, EnterpriseBaseObject 仍是合同的一部分,它的存在将是公开的。但只有成员标记 [数据成员] 将予以公布。

You handle inheritance by adding a [KnownType(typeof(ServiceObject))] to EnterpriseBaseObject - however, the EnterpriseBaseObject is still part of the contract, and its existance will be public. But only members marked [DataMember] will be published.

一个选项(删除继承)是有一个单独的DTO的序列化的目的,并有DTO版本和实际版本之间的转换 - 但是,使额外的工作。

One option (to remove the inheritance) is to have a separate DTO for serialization purposes, and have a conversion between the DTO version and the actual version - but that makes extra work.

这篇关于WCF序列化对象继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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