简体   繁体   English

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

[英]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. 我有一个通过wsdl.exe自动生成的类,我需要将[System.Xml.Serialization.XmlIgnoreAttribute()]属性添加到其中一个属性中,但我无法直接修改该类,因为它每次都重新生成时不时。

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. 由于客户的限制,我坚持使用.NET Framework 2.0。

(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) (更多详细信息为什么我需要在这里执行此操作: 防止在DateTime值的反序列化时进行时区转换 ,我在部分类中添加字符串属性)

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. 您可以使用postsharp动态地向属性添加缺少的属性。 Have a look at How to inject an attribute using a PostSharp attribute? 看看如何使用PostSharp属性注入属性? .

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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