I would like to pass a string parameter (name) in the following line. How do I do that?
string name = "First Name";
//How do I pass "First Name" in the function?
tempControl.GetType().GetMethod("LoadAnswers").Invoke(tempControl, null);
I would like to pass a string parameter (name) in the following line. How do I do that?
string name = "First Name";
//How do I pass "First Name" in the function?
tempControl.GetType().GetMethod("LoadAnswers").Invoke(tempControl, null);
You will need to pass the Invoke
method an object array of arguments:
tempControl.GetType().GetMethod("LoadAnswers").Invoke(tempControl, new object[] { name });
But... I am confused as to why you don't just call the method on the tempControl
object:
tempControl.LoadAnswers(name);
??
EDIT
As @Jason P mentioned in the comments, if you cast your control to the right type (for the sake of argument, MyUserControl
) you'll be able to access the method without using reflection. This would be a much more readable and performant solution:
var myControl = (MyUserControl)tempControl;
myControl.LoadAnswers(name);