tags:

views:

224

answers:

5

How could I go about calling a method on a static class given the class name and the method name, please?

For example:

Given "System.Environment" and "GetFolderPath", I'd like to use reflection to call Environment.GetFolderPath().

Thanks,

Dan

A: 

Here is a basic outline of what you would do:

  1. Scan all the objects in the current AppDomain - find the one that matches what you know the class name to be
  2. Get the static method with the name you know on that object
  3. Dynamically invoke it.

Edit: This will work if you do not know the namespace of the static class. Otherwise use Daniel Brückner's solution as its much simpler.

George Mauer
Why downvotes? Is this not right? I'm going to go check.
George Mauer
Why the downvotes indeed ... upvoted to counter ... trying it out now thanks George
Daniel Elliott
dtb
I think you still do need to scan the AppDomain - If you could just create an instance of a class this would be dynamic language
George Mauer
Type.GetType? http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx
dtb
This might be a literal description of what is occuring during the reflection process, however I don't think this answer is helpful to the OP because it does not practically demonstrate or explain how the steps you have outlined can be taken using the .net framework APIs
Crippledsmurf
You're right, didn't know about that - this is still useful if you don't know or don't want to type out the namespace but yeah Type.GetType in the 99% scenario.
George Mauer
+10  A: 

Just

Type.GetType(typeName).GetMethod(methodName).Invoke(null, arguments);

where typeName is the name of the type as a string, methodName is the name of the method as a string, and arguments is an array of objects containing the arguments to call the method with.

Daniel Brückner
Wow, ok yeah that works - didn't know about Type.GetType - just make sure that the Static class name is fully qualified with the namespace
George Mauer
A: 

System.Reflection.Assembly info = typeof(System.Environment).Assembly;

Type t = info.GetType("System.Environment"); MethodInfo m = t.GetMethod("GetFolderPath");

object result = m.Invoke(null, arguments);

rajesh pillai
+1  A: 

First you need to get the Type (by iterating on the assembly using reflection)

see this link for details: http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx

or use

Assembly.GetType

once you have the type in hand you can iterate over members using reflection or

MethodInfo method = typeof(MyClass).GetMethod("MyMethod");

then you can use MethodInfo.Invoke and pass arguments to invoke the method when you want to invoke it.

Ahmed Khalaf
A: 

What you are doing here is reflecting on the type named Environement and using the GetPropery and GetGetMethod methods to get the get method of the Environment.CurrentDirectory property like so

var getMethod = typeof(Environment).GetProperty("CurentDirectory", BindingFlags.Public | BindingFlags.Static).GetGetMethod();
var currentDirectory = (string)getMethod.Invoke(null, null);

Calling the get method of a property returns it's value and is equivilent to

var value = Environment.CurrentDirectory;
Crippledsmurf