Calling a method on a static class given it's type name and method names as strings?

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.

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 Sep 13 '09 at 17:07.

First you need to get the Type (by iterating on the assembly using reflection) see this link for details: msdn.microsoft.com/en-us/library/system.... 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.

Here is a basic outline of what you would do: Scan all the objects in the current AppDomain - find the one that matches what you know the class name to be Get the static method with the name you know on that object 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.

I'm going to go check. – George Mauer Sep 13 '09 at 16:57 Why the downvotes indeed ... upvoted to counter ... trying it out now thanks George – Daniel Elliott Sep 13 '09 at 16:59 1. If you know the type name, you don't have to "scan" the entire AppDomain; 2.

&3. That's the question – dtb Sep 13 '09 at 17:00 1 Type. GetType?

Msdn.microsoft.com/en-us/library/w3f99sx1.aspx – dtb Sep 13 '09 at 17:06 2 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 Sep 13 '09 at 17:09.

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.

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

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions