c# - 使用反射从 Soap Client 获取 Web 服务方法

标签 c# wcf web-services reflection

我正在尝试从仅包含 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/308359http://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/

相关文章:

具有传输和消息安全性的 WCF 绑定(bind)

web-services - 在域数据错误时返回哪个 HTTP 状态?

asp.net - 您可以在.Net 中锁定单个应用程序变量吗?

C# Selenium - Click/Clear/SendKeys 不能正常工作,但在 Debug模式下工作

c# - System.InvalidCastException:无法将类型为“System.Object”的对象转换为类型为“System.IO.StreamWriter”

c# - 在 WCF 中不使用 CallBack 将数据推送到客户端

wcf - 创建 Web 服务 (WCF) 以与 QuickBooks 集成

c# - 使用 WCF 在客户端中获取签名的 SOAP 消息

c# - 如何使用 SignalR Serverless 处理用户关闭浏览器?

c# - 如何使用 SharpZipLib 提取或访问 gzip 中特定文件的流?