2010-07-08 77 views
1

給定一個引用asmx的url我將如何去顯示他們所有的方法名? 如果程序集=「http://.../something/something.asmx」,我試圖顯示該服務的方法名稱,現在我應該怎麼做,我已經得到了自己這麼多?我似乎找到了數以百計的例子中找到一種解決香港專業教育學院看着如何在C#中使用反射來列出.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); 
     } 
    } 

回答

1

,所以我想出如何得到我想要它是這樣的

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

這正是我尋找。你爲我節省了很多時間。 – user1994514 2014-04-11 05:35:21

1
typeof(Object).GetMethods() 

的youre要求object

你需要調用的getMethods()你想要的類型,類型的所有方法得到的方法。

1

粘貼http://.../something/something.asmx在您的瀏覽器,它會給你一個所有的方法和它的參數列表?

1

試試這個:

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(...替換爲testAssembly.GetTypes(),並遍歷所有類型,並獲取每個類的方法。

1

你知道,拋開思考,你實際上可以查詢web服務的WSDL來獲取方法列表。它可能會簡化您的問題。如果您使用反射設置,則必須在程序集中找到類型,並使用其他答案中描述的其他方法來獲取方法。

1

您需要尋找從System.Web.Services.WebService繼承的類上使用[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); 
       } 
      } 
     } 
    } 
} 
相關問題