使用反射获取Web服务方法从SOAP客户端 [英] Getting webservice methods from Soap Client using reflection
问题描述
我想从仅包含web服务本身的方法的SOAPClient对象得到一个MethodInfo的集合。这是目前我在做什么。 。目前,它返回MyServiceSoapClient的所有方法
MyServiceSoapClient为myService =新MyServiceSoapClient();
的MethodInfo [] =方法myService.GetType()的getMethods();
在的getMethods()方法支持,您可以使用的BindingFlags更具体地选择你想要它返回的方法。看看这里:
的 http://msdn.microsoft.com/en-us/library/4d848zkb.aspx
此外,您还可以使用一些LINQ进一步说明什么你是后:
的MethodInfo [] =方法myService.GetType()的getMethods();
的MethodInfo [] = methodsOfWebservice methods.Where(M = GT; m.whatever ==无论&功放;&安培; m.anothercondition == TRUE); //等
您的最后一个选项是一个属性添加到你想让它每个方法返回然后测试该属性的存在。看看这里:
HTTP ://www.codeproject.com/KB/cs/attributes.aspx
更新2011-01-18
我看微软知识库,发现[的WebMethod]是一个属性。
http://support.microsoft.com/kb/308359 并的 http://msdn.microsoft.com/en-us/library/28a537td.aspx 。
当让所有方法,你可以测试这个属性的存在,以决定该方法是否是一个WebMethod与否。
列表与LT; MethodInfo的> methodsOfWebservice =新的List< MethodInfo的>();
的MethodInfo [] =方法myService.GetType()的getMethods();
的foreach(在方法MethodInfo的方法)
{
的foreach(在method.GetCustomAttributes属性属性(真))
{
如果(属性WebMethodAttribute)
methodsOfWebservice.Add(法);
}
}
更新2011-01-20
我只是测试下面的代码,它事实上确实给我的 WebMethodAttribute
在属性
变量:
键入类型= obj.GetType();
VAR方法= type.GetMethod(methodName的);
var属性= method.GetCustomAttributes(typeof运算(WebMethodAttribute),TRUE);
我敢肯定,你应该能够做同样的与您的代码和测试的存在在WebMethodAttribute
I'm trying to get a MethodInfo collection from a SOAPClient object that contains only the methods of the webservice itself. Here is what I'm currently doing. At the moment it returns all the methods of the MyServiceSoapClient.
MyServiceSoapClient myService = new MyServiceSoapClient();
MethodInfo[] methods = myService.GetType().GetMethods();
The GetMethods() method supports bindingflags that you can use to more specifically select the methods you want it to return. Have a look here:
http://msdn.microsoft.com/en-us/library/4d848zkb.aspx
Also, you could use some linq to further specify what you are after:
MethodInfo[] methods = myService.GetType().GetMethods();
MethodInfo[] methodsOfWebservice = methods.Where(m => m.whatever == whatever && m.anothercondition == true); // etc.
The last option you have is to add an Attribute to every method you want it to return and then test for the presence of the Attribute. Have a look here:
http://www.codeproject.com/KB/cs/attributes.aspx
Update 2011-01-18
I've looked at Microsoft KnowledgeBase and found that the [WebMethod] is an attribute. http://support.microsoft.com/kb/308359 and http://msdn.microsoft.com/en-us/library/28a537td.aspx. When getting all methods you could test for the presence of this attribute to decide whether the method is a WebMethod or not.
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);
}
}
Update 2011-01-20
I just tested the following code and it does in fact give me the the WebMethodAttribute
in the attribute
variable:
Type type = obj.GetType();
var method = type.GetMethod("methodname");
var attribute = method.GetCustomAttributes(typeof(WebMethodAttribute), true);
I'm sure you should be able to do the same with your code and test for the presence of the WebMethodAttribute
这篇关于使用反射获取Web服务方法从SOAP客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!