2013-03-13 55 views
3

我有一个类是通过wsdl.exe自动生成的,我需要将[System.Xml.Serialization.XmlIgnoreAttribute()]属性添加到其中一个属性,但我无法直接将该类修改为它不时再生。添加C#属性到wsdl.exe生成的属性?

有没有办法做到这一点?我已经尝试寻找继承,部分类和反射的解决方案,但没有运气。 由于客户的限制,我坚持使用.NET Framework 2.0。

(为什么我需要在这里做的更多细节:Prevent timezone conversion on deserialization of DateTime value,我将在部分类的字符串属性)

编辑:请求的代码段是一个简单的,因为这:

[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; 
    // } 
    //} 
} 
+0

为什么继承在这种情况下不起作用?你看了作文吗?请提供您的WSDL类的缩写片段以及您想要设置属性的属性。 – 2013-03-13 14:40:08

+0

我认为http://stackoverflow.com/questions/10174519/how-can-i-use-attributes-on-a-property-defined-in-the-other-half-of-a-partial-cl回答你的问题题。它使用MetadataType。 – 2013-03-13 14:42:42

+0

继承将无法工作,因为该对象是由服务调用实例化的,所以我仍然会得到基类。 – capitano666 2013-03-13 14:44:24

回答

0

您可以使用postsharp动态添加缺少的属性到属性。看看How to inject an attribute using a PostSharp attribute?

它显示了如何将XmlIgnore属性应用于每个公共属性,但是您可以更改方面代码以在您的情况下具有不同的行为。

+0

恐怕使用第三方框架不是一个选项:/ – capitano666 2013-03-13 15:05:10

+0

我认为你有太多的约束:) – 2013-03-13 15:14:48

+0

这是与作为顾问的问题;) 无论如何,如果第三方框架有可能,基础框架也应该是可能的(但可能更难),不是吗? – capitano666 2013-03-13 15:19:34