如何在服务代理上检测自动生成的方法参数?

时间:2013-02-07 15:46:43

标签: c# .net wcf service proxy

当我为我的服务创建代理时,一些额外的参数被添加为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?

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

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

1 个答案:

答案 0 :(得分: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代码gen(svcutil?)也许一些MSFT的设计者可能会更清楚。