本文介绍了如何使用反射在C#列表中的.asmx的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出的URL引用了一个ASMX如何将我去展示他们的所有方法的名字?
如果装配=HTTP://.../something/something.asmx我是想显示该服务的方法名称我应该现在我已经得到了迄今为​​止自己这个做什么?我似乎找到了数以百计的例子中解决香港专业教育学院看着

 公共TestReflection(串组件)
    {
        大会testAssembly = Assembly.LoadFrom(组装);
        键入STYPE = testAssembly.GetType();        MethodInfo的[] methodInfos = typeof运算(对象).GetMethods();
        的foreach(MethodInfo的MethodInfo的在methodInfos)
        {
            Console.WriteLine(methodInfo.Name);
        }
    }


解决方案

所以我想出如何得到我想要它去像这样

  [SecurityPermissionAttribute(SecurityAction.Demand,无限制= TRUE)]
内部静态无效LoadWebService(字符串webServiceAsmxUrl)
{
    的parseURL(webServiceAsmxUrl);
    System.Net.WebClient客户端=新System.Net.WebClient();
    //连接到Web服务
    的System.IO.Stream流= client.OpenRead(webServiceAsmxUrl +WSDL?);
    //现在读描述服务的WSDL文件。
    ServiceDescription描述= ServiceDescription.Read(流);
    /////加载DOM /////////
    //初始化服务描述进口国。
    ServiceDescriptionImporter进口商=新ServiceDescriptionImporter();
    importer.ProtocolName =SOAP12; //使用SOAP 1.2。
    importer.AddServiceDescription(描述,NULL,NULL);
    //生成代理客户端。
    importer.Style = ServiceDescriptionImportStyle.Client;
    //生成的属性重新present的原始值。
    进口商codeGenerationOptions =的System.Xml.Serialization codeGenerationOptions.GenerateProperties。;
    //初始化code-DOM树成,我们将导入服务。
    codeNamespace nmspace =新的codeNamespace();
    codeCompileUnit 1单元=新的codeCompileUnit();
    unit1.Namespaces.Add(nmspace);
    //导入服务到code-DOM树。这将创建一个使用服务代理code。
    ServiceDescriptionImportWarnings警告= importer.Import(nmspace,1单元);
    如果(警告== 0)//如果为零,那么,我们是好去
    {
        //生成代理code
        codeDomProvider provider1 = codeDomProvider.CreateProvider(CSHARP);
        //编译与适当的引用装配代理
        字符串[] = assemblyReferences新的字符串[5] {System.dll中,System.Web.Services.dll,System.Web.dll程序,system.xml.dll的,System.Data.dll中 };
        CompilerParameters PARMS =新CompilerParameters(assemblyReferences);
        CompilerResults结果= provider1.CompileAssemblyFromDom(PARMS,1单元);
        //检查是否存在错误
        如果(results.Errors.Count大于0)
        {
            的foreach(CompilerError哎呀在results.Errors)
            {
                System.Diagnostics.Debug.WriteLine(========编译器错误============);
                System.Diagnostics.Debug.WriteLine(oops.ErrorText);
            }
            Console.WriteLine(编译出错调用webservice的检查调试输出中的窗口。);
        }
        //最后,Web服务方法添加到我们的方法来测试列表
        // ------------------------------------------------ --------------------------------------------
        对象服务= results.CompiledAssembly.CreateInstance(服务名);
        输入类型= service.GetType();
        清单< MethodInfo的> 。listMethods = types.GetMethods()了ToList();
}
}

given a url that references an asmx how would i go about displaying all of their method names? 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? 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);
        }
    }
解决方案

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

这篇关于如何使用反射在C#列表中的.asmx的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 04:43