given a url that references an asmx how would i go about displaying all of their method names? if assembly="http://.../som开发者_StackOverflow社区ething/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);
}
}
typeof(Object).GetMethods()
youre asking for all the methods of type object
you need to call GetMethods() on the type you want to get the methods of.
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.
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.
You know, reflection aside, you can actually query the webservice's WSDL to get a list of methods. 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
.
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();
}
}
Paste http://.../something/something.asmx
in your browser and it will give you a list of all the methods and its parameters?
精彩评论