Can I prevent the deserialization of a specific datamember?

I have a datacontract like

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

    [DataMember]
    public string DM2;

    [DataMember]
    public string DM3;
}

and sometimes I want to prevent DM2 deserialization from returning from OperationContract. Something like that:

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

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

    return mdc;
}

I could always create a new DataContract that only has DM1 and DM3 and will generate this from an instance of MyDC, but I want to see if it is possible to remove DM2 programmatically. Is it possible? How?

+5
source share
4 answers
[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;
}
+2
source

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;
}

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

+6

, , .

, [DataContract] , [DataMember]:

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

    public string DM2;

    [DataMember]
    public string DM3;
}

[IgnoreDataMember] . (. http://msdn.microsoft.com/en-us/library/ms733127.aspx)

, : private, protected, internal, protected internal public. /, . . http://msdn.microsoft.com/en-us/library/aa347850.aspx.

+3
source

Yes, we can prevent attribute serialization. Put the annotation [DataContract]in the class and [DataMember]only for the serialized attribute. if you want to skip an attribute when the value of this attribute is zero, put [DataMember(EmitDefaultValue = false)]in this 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 non-zero, while others will be serialized in each condition.

0
source

All Articles