[英]Adding C# attribute to wsdl.exe generated property?
我有一個通過wsdl.exe自動生成的類,我需要將[System.Xml.Serialization.XmlIgnoreAttribute()]屬性添加到其中一個屬性中,但我無法直接修改該類,因為它每次都重新生成時不時。
有沒有辦法做到這一點? 我已經嘗試過尋找具有繼承,部分類和反射的解決方案,但沒有運氣。 由於客戶的限制,我堅持使用.NET Framework 2.0。
(更多詳細信息為什么我需要在這里執行此操作: 防止在DateTime值的反序列化時進行時區轉換 ,我在部分類中添加字符串屬性)
編輯:請求的代碼片段很簡單,如下所示:
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://common.ws.model.plx.ids.it/")]
public partial class publication {
private System.DateTime dateFromField;
//[System.Xml.Serialization.XmlIgnoreAttribute()] I would like to add this
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public System.DateTime dateFrom {
get {
return this.dateFromField;
}
set {
this.dateFromField = value;
}
}
///// This method has been moved in the other partial class
//[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, ElementName = "dateFrom")]
//public string dateFromString
//{
// get
// {
// return XmlConvert.ToString(dateFrom, XmlDateTimeSerializationMode.RoundtripKind);
// }
// set
// {
// dateFrom = DateTimeOffset.Parse(value).DateTime;
// }
//}
}
您可以使用postsharp動態地向屬性添加缺少的屬性。 看看如何使用PostSharp屬性注入屬性? 。
它顯示了如何將XmlIgnore屬性應用於每個公共屬性,但您可以更改方面代碼以在您的情況下具有不同的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.