【问题标题】:How to detect auto-generated method parameters on service proxies?如何检测服务代理上自动生成的方法参数?
【发布时间】:2013-02-07 15:46:43
【问题描述】:

当我为我的服务创建代理时,一些额外的参数被添加为 XmlIgnoreAttribute。例如:

接口方法契约声明:

[OperationContract]
void AddToList(int integerToAdd);

方法实现:

public void AddToList(int integerToAdd) {
    intList.Add(integerToAdd);
}

现在,当我动态地为我的服务生成代理时,这个方法实际上是这样的:

/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/IService1/AddToList", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public void AddToList(int integerToAdd, [System.Xml.Serialization.XmlIgnoreAttribute()] bool integerToAddSpecified)
{
    this.Invoke("AddToList", new object[] {integerToAdd, integerToAddSpecified});
}

如果我有此参数的 ParameterInfo 对象,如何检查此参数是否已标记为 XmlIgnoreAttribute?

如果这不是检查这些自动生成参数的好方法,那么还有其他(更好的)方法来检查它们吗?

奖励:是否会出现生成非布尔类型并将其标记为 XmlIgnoreAttribute 的情况?

【问题讨论】:

    标签: c# .net wcf service proxy


    【解决方案1】:

    我假设你会使用 ParameterInfo.GetCustomAttributes()

    ParameterInfo secondParam = ..
    object[] attribs = 
              secondParam.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
    //the second bool param is ignored. See the docs.
    //check if => attribs has anything..
    

    (1) 我不确定您为什么要检查这些参数,所以不确定它是否好/更好。 (2) 不确定第二个问题。我假设这是来自 VS 代码生成 (svcutil?) 也许一些设计这个的 MSFT 可能更了解。

    【讨论】:

    • 我在调用方法,boolean是必填参数,我设置整数的时候需要设置为true,所以想找个方法来检测这种类型的属性并设置.谢谢。我在自定义属性上找到了它,但是,话虽如此,你对奖金问题有什么看法吗? LOL 是否只生成了标记为 XmlIgnoreAttribute 的布尔类型?
    • @Alexandru 这就是我在(2) 之后的回答中的意思。您可能会更幸运地从构建 svcutil 的 MS 团队中找到一些人或认识某人的人,然后给他们发电子邮件。我已经这样做了很多次,如果你彻底解决了:D
    猜你喜欢
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多