将C#属性添加到wsdl.exe生成的属性?

时间:2013-03-13 14:36:23

标签: c# c#-2.0 wsdl.exe

我有一个通过wsdl.exe自动生成的类,我需要将 [System.Xml.Serialization.XmlIgnoreAttribute()] 属性添加到其中一个属性中,但我可以' t,不时地重新生成类。

有没有办法做到这一点?我已经尝试过寻找具有继承,部分类和反射的解决方案,但没有运气。 由于客户的限制,我坚持使用.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;
    //    }
    //}
}

1 个答案:

答案 0 :(得分:0)

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

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

相关问题