Is there a way to use Action to call a method based on a string value that contains the name of that method?
Asked
Active
Viewed 159 times
0
-
You added the dynamic tag to this question, so are you asking about the new dynamic keyword in .NET 4? Do you have some code that you tried and doesn't work? Might help to see the context of what you're trying to do. – CoderDennis Nov 28 '09 at 19:56
2 Answers
5
Action<T>
is just a delegate type that could point to a given method. If you want to call a method whose name is known only at runtime, stored in a string variable, you need have to use reflection:
class Program
{
static void Main(string[] args)
{
string nameOfTheMethodToCall = "Test";
typeof(Program).InvokeMember(
nameOfTheMethodToCall,
BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static,
null,
null,
null);
}
static void Test()
{
Console.WriteLine("Hello from Test method");
}
}
As suggested by @Andrew you could use Delegate.CreateDelegate to create a delegate type from a MethodInfo:
class Program
{
static void Main(string[] args)
{
string nameOfTheMethodToCall = "Test";
var mi = typeof(Program).GetMethod(nameOfTheMethodToCall, BindingFlags.NonPublic | BindingFlags.InvokeMethod | BindingFlags.Static);
var del = (Action)Delegate.CreateDelegate(typeof(Action), mi);
del();
}
static void Test()
{
Console.WriteLine("Hello from Test method");
}
}

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-
If you use `Delegate.CreateDelegate` you can create a delegate for your `MethodInfo` like the OP requested. – Andrew Hare Nov 28 '09 at 19:59
-
Great remark @Andrew. I've updated my answer to give an example of this. – Darin Dimitrov Nov 28 '09 at 20:02
2
I don't think you really want an Action<T>
just a regular method.
public void CallMethod<T>(T instance, string methodName) {
MethodInfo method = typeof(T).GetMethod(methodName);
if (method != null) {
method.Invoke(instance, null);
}
}

Bob
- 97,670
- 29
- 122
- 130