4

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;
    //    }
    //}
}
Community
  • 1
  • 1
capitano666
  • 656
  • 1
  • 9
  • 24
  • Why would inheritance not work in this case? And have you looked at composition? Please supply an abbreviated snippet of your WSDL class and the property you would like to set the attribute on. – Paul Sasik Mar 13 '13 at 14:40
  • I think http://stackoverflow.com/questions/10174519/how-can-i-use-attributes-on-a-property-defined-in-the-other-half-of-a-partial-cl answers your question. It uses MetadataType. – L-Four Mar 13 '13 at 14:42
  • Inheritance wouldn't work because the object is instantiated by the service call, so I would still get the base class. – capitano666 Mar 13 '13 at 14:44
  • MetadataType are available starting .NET Framework 3.5 :( – capitano666 Mar 13 '13 at 14:45

1 Answers1

1

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.

Community
  • 1
  • 1
L-Four
  • 13,345
  • 9
  • 65
  • 109
  • That's the problem with being a consultant ;) Anyhow, if it is possible with a 3rd party framework it should also be possible (but maybe harder) with the base framework, shouldn't it? – capitano666 Mar 13 '13 at 15:19