I have several methods all with the same signature (parameters and return values) but different names and the internals of the methods are different. I want to pass the name of the method to run to another method that will invoke the passed in method.
public int Method1(string)
{
... do something
return myInt;
}
public int Method2(string)
{
... do something different
return myInt;
}
public bool RunTheMethod([Method Name passed in here] myMethodName)
{
... do stuff
int i = myMethodName("My String");
... do more stuff
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
This code does not work but this is what I am trying to do. What I don't understand is how to write the RunTheMethod code since I need to define the parameter.
You can also try Action Delegate!
And then call your method using
Or
Then simply call method
Usage:
If you want the ability to change which method is called at run time I would recommend using a delegate: http://www.codeproject.com/KB/cs/delegates_step1.aspx
It will allow you to create an object to store the method to call and you can pass that to your other methods when it's needed.
While the accepted answer is absolutely correct, I would like to provide an additional method.
I ended up here after doing my own searching for a solution to a similar question. I am building a plugin driven framework, and as part of it I wanted people to be able to add menu items to the applications menu to a generic list without exposing an actual
Menu
object because the framework may deploy on other platforms that don't haveMenu
UI objects. Adding general info about the menu is easy enough, but allowing the plugin developer enough liberty to create the callback for when the menu is clicked was proving to be a pain. Until it dawned on me that I was trying to re-invent the wheel and normal menus call and trigger the callback from events!So the solution, as simple as it sounds once you realize it, eluded me until now.
Just create separate classes for each of your current methods, inherited from a base if you must, and just add an event handler to each.
You need to use a delegate. In this case all your methods take a
string
parameter and return anint
- this is most simply represented by theFunc<string, int>
delegate1. So your code can become correct with as simple a change as this:Delegates have a lot more power than this, admittedly. For example, with C# you can create a delegate from a lambda expression, so you could invoke your method this way:
That will create an anonymous function like this:
and then pass that delegate to the
RunTheMethod
method.You can use delegates for event subscriptions, asynchronous execution, callbacks - all kinds of things. It's well worth reading up on them, particularly if you want to use LINQ. I have an article which is mostly about the differences between delegates and events, but you may find it useful anyway.
1 This is just based on the generic
Func<T, TResult>
delegate type in the framework; you could easily declare your own:and then make the parameter be of type
MyDelegateType
instead.Here is an example Which can help you better to understand how to pass a function as a parameter.
Suppose you have Parent page and you want to open a child popup window. In the parent page there is a textbox that should be filled basing on child popup textbox.
Here you need to create a delegate.
Parent.cs // declaration of delegates public delegate void FillName(String FirstName);
Now create a function which will fill your textbox and function should map delegates
Now on button click you need to open a Child popup window.
IN ChildPopUp constructor you need to create parameter of 'delegate type' of parent //page
ChildPopUp.cs