I have a method in my user control:
public string ControleIdContainer()
{
string abc = "Hello";
return abc;
}
Now I want to call this method on my Page using reflection. I have tried this but it is not working:
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/UserControls"));
FileInfo[] controlsInfo = dirInfo.GetFiles("*.ascx");
foreach (var item in controlsInfo)
{
var customControl = LoadControl(string.Format("~/UserControls/{0}", item));
var controlType = customControl.GetType();
var controlClientScript = controlType.GetMethod("ControleIdContainer").Invoke(null,null);
}
The first parameter of
MethodInfo.Invokeis the instance on which you want to invoke the method. PasscustomControlas the first parameter instead ofnulland it should work.