【发布时间】:2013-03-01 13:32:08
【问题描述】:
我有一个通过 wsdl.exe 自动生成的类,我需要将 [System.Xml.Serialization.XmlIgnoreAttribute()] 属性添加到其中一个属性,但我可以' t 直接修改类,因为它时不时地重新生成。
有没有办法做到这一点?我已经尝试使用继承、部分类和反射寻找解决方案,但没有运气。 由于客户的限制,我坚持使用 .NET Framework 2.0。
(关于为什么我需要在此处执行此操作的更多详细信息:Prevent timezone conversion on deserialization of DateTime value,我正在将字符串属性添加到部分类中)
编辑:请求的代码 sn-p 很简单:
[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;
// }
//}
}
【问题讨论】:
-
为什么在这种情况下继承不起作用?你看过作文吗?请提供您的 WSDL 类的缩写 sn-p 和您想要设置属性的属性。
-
我认为stackoverflow.com/questions/10174519/… 回答了您的问题。它使用 MetadataType。
-
继承不起作用,因为对象是由服务调用实例化的,所以我仍然会得到基类。
-
MetadataType 从 .NET Framework 3.5 开始可用 :(