2012-12-30 14 views

答えて

8

DataContractを使用する主な利点は、XmlMediaTypeFormatterJsonMediaTypeFormatterのいくつかの一般的なシリアライズヒントで重複する属性を避けることができることです。私。シリアライズするモデルの特定のプロパティをオプトイン/オプトアウトしたり、プロパティの名前を変更して両方のフォーマッタに尊重したりすることができます。例えば

[DataContract] 
public class Sample { 

    [DataMember] 
    public string PropOne {get;set;} 

    public string PropTwo {get;set;} 

    [DataMember(Name="NewName")] 
    public string PropThree {get; set;} 
} 

と等価である:

public class Sample { 
    public string PropOne {get;set;} 

    [XmlIgnore] 
    [JsonIgnore] 
    public string PropTwo {get;set;} 

    [JsonProperty(PropertyName = "NewName")] 
    [XmlElement("NewName")] 
    public string PropThree {get; set;} 
} 
関連する問題