简体   繁体   English

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

[英]how to use reflection in C# to list the methods of an .asmx

given a url that references an asmx how would i go about displaying all of their method names? 给定引用asmx的url,我将如何显示其所有方法名称? if assembly="http://.../something/something.asmx" and i was trying to display the method names of that service what should i do now that i have gotten myself this far? 如果assembly =“ http://.../something/something.asmx”并且我试图显示该服务的方法名称,那么到目前为止我该怎么办? i cant seem to find a solution among the hundreds of examples ive looked at 我似乎无法在我看过的数百个示例中找到解决方案

   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()

youre asking for all the methods of type object 您要求类型object所有方法

you need to call GetMethods() on the type you want to get the methods of. 您需要在要获取其方法的类型上调用GetMethods()。

Try this: 尝试这个:

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);
    }
}

The idea is that in your original code, you're trying to get the methods by using typeof(Object) , which will retrieve the methods of the Object type, which is not what you want. 这个想法是,在您的原始代码中,您尝试使用typeof(Object)来获取方法,该方法将检索Object类型的方法,这不是您想要的。

You need to know what class the methods you're trying to grab are in. If you don't know that, replace testAssembly.GetType(... with testAssembly.GetTypes() and iterate through all the types, and getting the methods for each one. 您需要知道要尝试获取的方法属于哪个类。如果不知道,请将testAssembly.GetType(...替换为testAssembly.GetTypes()并遍历所有类型,然后获取方法每一个人。

You know, reflection aside, you can actually query the webservice's WSDL to get a list of methods. 除了反射,您实际上可以查询Web服务的WSDL以获得方法列表。 It may simplify your problem. 它可以简化您的问题。 If you're set on using reflection you'll have to find the type in the assembly and grab the methods using the other methods described in the other answers here. 如果您打算使用反射,则必须在程序集中找到类型,然后使用此处其他答案中所述的其他方法来获取方法。

You would need to look for method decorated with the [WebMethod] attribute on classes that inherit from System.Web.Services.WebService . 您需要在从System.Web.Services.WebService继承的类上寻找用[WebMethod]属性修饰的方法。

The code would look something like this (not tested): 代码看起来像这样(未经测试):

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);
                }
            }
        }
    }
}

so i figured out how to get what i wanted it goes something like this 所以我想出了如何得到我想要的东西

[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<MethodInfo> listMethods = types.GetMethods().ToList();
}
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM