【问题标题】:Adding C# attribute to wsdl.exe generated property?将 C# 属性添加到 wsdl.exe 生成的属性?
【发布时间】:2013-03-01 13:32:08
【问题描述】:

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

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

(关于为什么我需要在此处执行此操作的更多详细信息:Prevent timezone conversion on deserialization of DateTime value,我正在将字符串属性添加到部分类中)

编辑:请求的代码 sn-p 很简单:

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

【问题讨论】:

  • 为什么在这种情况下继承不起作用?你看过作文吗?请提供您的 WSDL 类的缩写 sn-p 和您想要设置属性的属性。
  • 我认为stackoverflow.com/questions/10174519/… 回答了您的问题。它使用 MetadataType。
  • 继承不起作用,因为对象是由服务调用实例化的,所以我仍然会得到基类。
  • MetadataType 从 .NET Framework 3.5 开始可用 :(

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


【解决方案1】:

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

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

【讨论】:

  • 这就是作为顾问的问题 ;) 无论如何,如果可以使用 3rd 方框架,它也应该可以(但可能更难)使用基础框架,不是吗?跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 2013-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多