简体   繁体   中英

Adding C# attribute to wsdl.exe generated property?

I have a class which is automatically generated via wsdl.exe, I need to add the [System.Xml.Serialization.XmlIgnoreAttribute()] attribute to one of the properties, but I can't modify the class directly as it is regenerated every now and then.

Is there any way to do this? I already tried searching for solutions with inheritance, partial classes and reflection but without luck. I'm stuck with .NET Framework 2.0 because of customer's constraints.

(more details on why I need to do this here: Prevent timezone conversion on deserialization of DateTime value , I'm adding the string property in a partial class)

EDIT: A requested code snippet is a simple as this:

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

You can use postsharp to dynamically add missing attributes to properties. Have a look at How to inject an attribute using a PostSharp attribute? .

It shows how you can apply the XmlIgnore attribute to every public property, but you can change the aspect code to have different behavior in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM