如何在C#中使用reflection来列出.asmx的方法

给定一个引用asmx的url我将如何显示所有方法名称? 如果assembly =“http://…/something/something.asmx”,我试图显示该服务的方法名称,我现在应该怎样做才能让自己走得这么远? 我似乎无法在我看过的数百个例子中找到解决方案

public TestReflection(string assembly) { Assembly testAssembly = Assembly.LoadFrom(assembly); Type sType = testAssembly.GetType(); MethodInfo[] methodInfos = typeof(Object).GetMethods(); foreach (MethodInfo methodInfo in methodInfos) { Console.WriteLine(methodInfo.Name); } } 

 typeof(Object).GetMethods() 

你要求所有类型object的方法

你需要在你想要获得方法的类型上调用GetMethods()。

在浏览器中粘贴http://.../something/something.asmx ,它会为您提供所有方法及其参数的列表?

试试这个:

 public TestReflection(string assembly) { Assembly testAssembly = Assembly.LoadFrom(assembly); Type sType = testAssembly.GetType("NamespaceOfYourClass.NameOfYourClassHere", true, true); MethodInfo[] methodInfos = sType.GetMethods(); foreach (MethodInfo methodInfo in methodInfos) { Console.WriteLine(methodInfo.Name); } } 

我们的想法是,在您的原始代码中,您尝试使用typeof(Object)来获取方法,这将检索Object类型的方法,这不是您想要的。

你需要知道你想要抓取的方法是什么类。如果你不知道,替换testAssembly.GetType(... with testAssembly.GetTypes()并遍历所有类型,并获取方法每一个人。

您知道,除了reflection之外,您实际上可以查询Web服务的WSDL以获取方法列表。 它可以简化您的问题。 如果您已设置使用reflection,则必须在程序集中找到类型,并使用此处其他答案中描述的其他方法来获取方法。

您需要在从System.Web.Services.WebServiceinheritance的类中查找使用[WebMethod]属性修饰的方法。

代码看起来像这样(未经测试):

 public TestReflection(string assembly) { Assembly testAssembly = Assembly.LoadFrom(assembly); // or .LoadFile() here foreach (Type type in testAssembly.GetTypes()) { if (type.IsSubclassOf(typeof(System.Web.Services.WebService))) { foreach (MethodInfo methodInfo in type.GetMethods()) { if (Attribute.GetCustomAttribute(methodInfo, typeof(System.Web.Services.WebMethodAttribute)) != null) { Console.WriteLine(methodInfo.Name); } } } } } 

所以我想出了如何得到我想要的东西就像这样

 [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)] internal static void LoadWebService(string webServiceAsmxUrl) { ParseUrl(webServiceAsmxUrl); System.Net.WebClient client = new System.Net.WebClient(); // Connect To the web service System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl"); // Now read the WSDL file describing a service. ServiceDescription description = ServiceDescription.Read(stream); ///// LOAD THE DOM ///////// // Initialize a service description importer. ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); importer.ProtocolName = "Soap12"; // Use SOAP 1.2. importer.AddServiceDescription(description, null, null); // Generate a proxy client. importer.Style = ServiceDescriptionImportStyle.Client; // Generate properties to represent primitive values. importer.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties; // Initialize a Code-DOM tree into which we will import the service. CodeNamespace nmspace = new CodeNamespace(); CodeCompileUnit unit1 = new CodeCompileUnit(); unit1.Namespaces.Add(nmspace); // Import the service into the Code-DOM tree. This creates proxy code that uses the service. ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1); if (warning == 0) // If zero then we are good to go { // Generate the proxy code CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp"); // Compile the assembly proxy with the appropriate references string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll", "System.Data.dll" }; CompilerParameters parms = new CompilerParameters(assemblyReferences); CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1); // Check For Errors if (results.Errors.Count > 0) { foreach (CompilerError oops in results.Errors) { System.Diagnostics.Debug.WriteLine("========Compiler error============"); System.Diagnostics.Debug.WriteLine(oops.ErrorText); } Console.WriteLine("Compile Error Occured calling webservice. Check Debug ouput window."); } // Finally, add the web service method to our list of methods to test //-------------------------------------------------------------------------------------------- object service = results.CompiledAssembly.CreateInstance(serviceName); Type types = service.GetType(); List listMethods = types.GetMethods().ToList(); } }