我可以阻止特定数据成员被反序列化吗?

发布于 2024-09-11 20:49:27 字数 545 浏览 15 评论 0原文

我有一个像这样的数据合约

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    [DataMember]
    public string DM2;

    [DataMember]
    public string DM3;
}

有时我想防止 DM2 在从操作合约返回时被反序列化。像这样的事情:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being deserialized  
    }

    return mdc;
}

我总是可以创建一个仅包含 DM1 和 DM3 的新 DataContract 并从 MyDC 实例生成它,但我想看看是否可以以编程方式删除 DM2。是否可以?如何?

I have a datacontract like this

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    [DataMember]
    public string DM2;

    [DataMember]
    public string DM3;
}

and sometimes I want to prevent DM2 from being deserialized when being returned from an OperationContract. Something like this:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being deserialized  
    }

    return mdc;
}

I could always make a new DataContract that has only DM1 and DM3 and generate that from the MyDC instance but I want to see if it is possible to programatically remove DM2. Is it possible? How?

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

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

发布评论

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

评论(4

两仪 2024-09-18 20:49:27

执行此操作的一种方法是将 DataMemberAttribute 的 EmitDefaultValue 属性设置为 false:

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    [DataMember(EmitDefaultValue = false)]
    public string DM2;

    [DataMember]
    public string DM3;
}

然后将此属性设置为 null:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being deserialized  
        mdc.DM2 = null;
    }

    return mdc;
}

这样,该属性就不会在序列化时写入输出流。

http://msdn.microsoft.com/en-us/library/aa347792.aspx

One way to do this is to set the EmitDefaultValue property of the DataMemberAttribute to false:

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    [DataMember(EmitDefaultValue = false)]
    public string DM2;

    [DataMember]
    public string DM3;
}

Then setting this property to null:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being deserialized  
        mdc.DM2 = null;
    }

    return mdc;
}

This way, that property doesn't get written to the output stream on serialization.

http://msdn.microsoft.com/en-us/library/aa347792.aspx

帅的被狗咬 2024-09-18 20:49:27

你的意思是序列化而不是反序列化。

只有具有 [DataMember] 属性的类的成员才会被序列化:

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    public string DM2;

    [DataMember]
    public string DM3;
}

如果您准备一个用于序列化的类,并将 [DataContract] 属性应用于该类,则 复杂情况下使用[IgnoreDataMember]可以解决您的问题。 (请参阅http://msdn.microsoft.com/en-us/library/ ms733127.aspx

顺便说一句,您可以序列化字段和属性,而不管可访问性如何:privateprotectedinternal受保护的内部公共。您可以序列化任何读/写属性,而不仅仅是字段。有关集合类型的序列化,请参阅 http://msdn.microsoft.com/en-我们/library/aa347850.aspx

What you mean is serialization and not deserialization.

If you prepare a class for serialization applying the [DataContract] attribute to the class, only the members of the class that has [DataMember] attribute will be serialized:

[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    public string DM2;

    [DataMember]
    public string DM3;
}

In some more complex cases the usage of [IgnoreDataMember] can solve your problem. (See http://msdn.microsoft.com/en-us/library/ms733127.aspx)

By the way, you can serialize fields and properties, regardless of accessibility: private, protected, internal, protected internal, or public. You can serialize any read/write properties and not only fields. About serialization of collection types see http://msdn.microsoft.com/en-us/library/aa347850.aspx.

两仪 2024-09-18 20:49:27
[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    public string DM2;

    public bool IsDM2Serializable;

    [DataMember(Name="DM2", EmitDefaultValue = false)]
    public string DM2SerializedConditionally
    {
        get
        {
            if(IsDM2Serializable)
                return null;
            return DM2;
        }
        set { DM2=value; }
    }

    [DataMember]
    public string DM3;
}

然后在需要隐藏时将 IsDM2Serializable 设置为 false:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being serialized  
        mdc.IsDM2Serializable = false;
    }

    return mdc;
}
[DataContract]
class MyDC 
{
    [DataMember]
    public string DM1;

    public string DM2;

    public bool IsDM2Serializable;

    [DataMember(Name="DM2", EmitDefaultValue = false)]
    public string DM2SerializedConditionally
    {
        get
        {
            if(IsDM2Serializable)
                return null;
            return DM2;
        }
        set { DM2=value; }
    }

    [DataMember]
    public string DM3;
}

Then set IsDM2Serializable to false when you need to hide it:

[OperationContact]
public MyDC GetMyDC()
{
    MyDC mdc = new MyDC();

    if (condition)
    {
        // Code to prevent DM2 from being serialized  
        mdc.IsDM2Serializable = false;
    }

    return mdc;
}
So尛奶瓶 2024-09-18 20:49:27

是的,我们可以阻止属性序列化。
[DataContract] 注释放在类上,并将 [DataMember] 仅用于序列化属性。如果您想在属性值为 null 时跳过该属性,请在该属性上放置 [DataMember(EmitDefaultValue = false)]

示例:

[DataContract]
public class MyClass 
{
    [DataMember]
    public int Id{ get; set; } 
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string MessageBody { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public DateTime SentOn { get; set; } 
}

注意:SentOn 当不为空时将被序列化,其他的将在所有条件下被序列化。

Yes, we can prevent an attribute from serialization.
Put [DataContract] Annotation on class and [DataMember] for only serialized attribute. if you want to skip attribute when that attribute value is null then put [DataMember(EmitDefaultValue = false)] on that attribute.

Example:

[DataContract]
public class MyClass 
{
    [DataMember]
    public int Id{ get; set; } 
    [DataMember]
    public string Title { get; set; }
    [DataMember]
    public string MessageBody { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public DateTime SentOn { get; set; } 
}

Note: SentOn will be serialized when it is not null and others will be serialized in every condition.

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