我正在尝试从仅包含 Web 服务本身的方法的 SOAPClient 对象获取 MethodInfo 集合。这是我目前正在做的事情。目前它返回 MyServiceSoapClient 的所有方法。
MyServiceSoapClient myService = new MyServiceSoapClient();
MethodInfo[] methods = myService.GetType().GetMethods();
最佳答案
GetMethods() 方法支持绑定(bind)标志,您可以使用它来更具体地选择您希望它返回的方法。看看这里:
http://msdn.microsoft.com/en-us/library/4d848zkb.aspx
此外,您可以使用一些 linq 来进一步指定您想要的内容:
MethodInfo[] methods = myService.GetType().GetMethods();
MethodInfo[] methodsOfWebservice = methods.Where(m => m.whatever == whatever && m.anothercondition == true); // etc.
最后一个选项是向您希望其返回的每个方法添加一个属性,然后测试该属性是否存在。看看这里:
http://www.codeproject.com/KB/cs/attributes.aspx
更新2011-01-18
我查看了 Microsoft 知识库,发现 [WebMethod] 是一个属性。 http://support.microsoft.com/kb/308359和 http://msdn.microsoft.com/en-us/library/28a537td.aspx 。 当获取所有方法时,您可以测试此属性是否存在,以确定该方法是否是 WebMethod。
List<MethodInfo> methodsOfWebservice = new List<MethodInfo>();
MethodInfo[] methods = myService.GetType().GetMethods();
foreach(MethodInfo method in methods)
{
foreach (Attribute attribute in method.GetCustomAttributes(true))
{
if (attribute is WebMethodAttribute)
methodsOfWebservice.Add(method);
}
}
更新2011-01-20
我刚刚测试了以下代码,它实际上为我提供了 attribute
变量中的 WebMethodAttribute
:
Type type = obj.GetType();
var method = type.GetMethod("methodname");
var attribute = method.GetCustomAttributes(typeof(WebMethodAttribute), true);
我确信您应该能够对代码执行相同的操作并测试 WebMethodAttribute 是否存在
关于c# - 使用反射从 Soap Client 获取 Web 服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715456/