排除 WCF DataContract 上的属性 [英] Exclude property on WCF DataContract

查看:52
本文介绍了排除 WCF DataContract 上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定这样的 WCF 接口定义,有没有办法从 ComplexObject 响应值中排除属性?

Given a WCF interface definition like this, is there a way to exclude a property from the ComplexObject response value?

我想排除 ChildObjects 属性.我不想从属性定义中删除 DataMember 属性,因为我需要在另一种情况下对其进行序列化.

I want to exclude the ChildObjects property. I don't want to remove the DataMember attribure from the property definition as I need it to be serialized in another case.

[ServiceContract]
public interface IComplexObjectService
{
    [OperationContract]
    ComplexObject Test(int a);
}

ComplexObject 的定义如下:

ComplexObject is defined something like this:

[DataContract(IsReference = true)]
public class ComplexObject 
{
    [DataMember]
    public long ObjectCode
    {
        get { return _ObjectCode; }
        set { _ObjectCode = value; }
    }

    [DataMember]
    public List<ComplexObject> ChildObjects
    {
        get { return _ComplexObject; }
        set { _ComplexObject = value; }
    }
}

推荐答案

如果您不想在 ComplexObject 中公开 ChildObjects 属性,则必须删除 DataMember 属性.如果您有另一个需要 ChildObjects 的用例,那么我建议您有一个单独的 ComplextObject 确实有它.您不能只是在运行时打开或关闭它,因为它会违反合同定义.

You are going to have to remove the DataMember attribute if you don't want to expose the ChildObjects property in your ComplexObject. If you have another use case which requires the ChildObjects then I suggest you have a separate ComplextObject which does have it. You cannot just switch it on or off at run-time as it will violate the contract definition.

这篇关于排除 WCF DataContract 上的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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