0

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);
MrM
  • 21,709
  • 30
  • 113
  • 139

1 Answers1

1

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);
Simon
  • 2,810
  • 2
  • 18
  • 23
  • I'm using a user control so LoadAnswers is on an ascx page tempControl = LoadControl("~/Controls/Questionnaire/Qst_rev1.ascx"); – MrM Jul 12 '13 at 18:54
  • 1
    @MrM cast `tempControl` to the actual type, and you'll be able to access its public members without reflection. – Jason P Jul 12 '13 at 19:01