Reputation: 83
I have a class (MethodsClass) with methods signature:
public double Method()
And a DataGrid
with ComboBoxColumn
. The ComboBox has the names of all methods of MethodsClass.
At runtime I need to invoke the method chosen from ComboBox. I am not very good with reflection and I know it is slow. Can I create an instance of MethodsClass using an ordinary initialisation
var mClass = new MethodsClass();
and then somehow invoke the method I need using a string from ComboBox. If not - what is the best way to implement the task.
Upvotes: 1
Views: 93
Reputation: 887547
You should create a Dictionary<string, Action<...>>
containing delegates (either method names or lambda expressions) to execute for each string value.
This is much faster than reflection, and also allows you to decouple the string values and parameters from the method signatures.
Upvotes: 2