何時使用 DataContract 和 DataMember 屬性? - When to use DataContract and DataMember attributes?

問題:

I am very confused about the DataContract attribute in WCF.我對 WCF 中的DataContract屬性感到非常困惑。 As per my knowledge it is used for serializating user defined type like classes.據我所知,它用於序列化用戶定義的類型,如類。 I wrote one class which is exposed at client side like this.我寫了一個這樣在客戶端公開的類。

[DataContract]
public class Contact
{
    [DataMember]
    public int Roll { get; set; }

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

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

    [DataMember]
    public int Age { get; set; }
}

It is working properly but when I remove DataContract and DataMember it also works properly.它工作正常,但是當我刪除DataContractDataMember它也可以正常工作。 I can't understand why it is working properly.我不明白爲什麼它可以正常工作。 Can any one tell me what is the actual use of DataContract ?誰能告訴我DataContract的實際用途是什麼?

My service contract looks like this我的服務合同看起來像這樣

[ServiceContract]    
public interface IRestServiceImpl
{
    [OperationContract]        
    Contact XmlData(string id);      
}

解決方案:

參考一: https://en.stackoom.com/question/KIF1
參考二: https://stackoom.com/question/KIF1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章