是否有任何開關指示svcutil
使用代碼中定義的名稱生成DataContract
屬性?例如,當我創建一個使用代理以下DataContract
:使用svcutil生成代理期間的DataContract屬性名稱
[DataContract(Namespace = "http://schemas.mynamespace.com/2012/08")]
public class MyDataContract
{
[DataMember(IsRequired = true, Order = 0)]
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
}
我得到這個DataContract
上的代理生成的類:
public partial class MyDataContract : object
{
private int _idField;
[System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
public int _id
{
get
{
return this._idField;
}
set
{
this._idField = value;
}
}
}
的DataMemberAttribute
的順序屬性始終中省略,以及對前3個屬性和MessageContract
省略了IDisposable
實現。
啊,對。這在WCF關於IDisposable實現的原則中也有規定。愚蠢的我,不去想它。 雖然這是首選方式,但關於'Property'的名字?只有公共'Properties'可以應用'DataContractAttribute'而不使用私有字段,或者保留私有字段並直接在'Property'上應用attr? – Pantelis
另一個想法是,自'Attribute'應用到'Property'上後保留私有字段是沒有意義的,是嗎? – Pantelis
我建議使用簡單的自動屬性。爲什麼?我上次檢查時,WCF無法使用完全不可變的DataContract類型,而不變性是自己寫出私有(只讀)字段的最常見原因(當然還有其他一些,如緩存魔法等)。 – gimpf