Calling methods using variables
hello,
I was wondering if there might be a way to call a mthod which name is stored in a variable.
I.E.
string s_method = "my_method";
Would it be possible to call "my_method" by using s_method without knowing what it contained?
Thanks for any help.
Bhakeeswaran ThulasingamPosted Aug 7, 2006, 1:37 AM
Here is an example :
System;using
using System.Reflection;
public class CallMethodUsingName
{
public static void Main ()
{
// Loads the assembly into the current application domain.
Assembly objAssm = Assembly.LoadFrom("ThreadSample.exe"); //Fully qualified namespace with class name
Type objType = objAssm.GetType("ThreadSample.Class1"); //Name of the method to be called
MethodInfo objMethod = objType.GetMethod("StartThread"); //Creates an instance of the class.
Object objInst = Activator.CreateInstance(objType); //Execute the method
objMethod.Invoke(objInst,null);
}
}
Bhakeeswaran ThulasingamPosted Aug 6, 2006, 7:18 AM