|
private void btnExecute_Click(object sender, System.EventArgs e)
{
// If the user is Japanese
dynamicExecution( “MyNameSpace.MyClass”, “OhaioGozaimasu”,”SayuriSan”);
// If the user is English
dynamicExecution( “MyNameSpace.MyClass”, “GoodMorning”,”Alice”);
// If the user is Indian
dynamicExecution( “MyNameSpace.MyClass35″, “Suprabhat”,”3sha”);
}
private void dynamicExecution(string ClassName,string FunctionName, string ClientName )
{
// Following code creates the instance of class dynamically and executes the function
// You will need the instance of assembly which has the class to be instatiated
// System.Reflection.Assembly.LoadFrom() this also can be used
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
Object [] para = new Object[1]; // array for parameters of the function
para[0]= ClientName ; // single parameter in this case
// Create instance of the class Dynamically
// CreateInstance requires FullPath i.e. with root namespace and sub namespaces of the type
object dynMyClass = assembly.CreateInstance(ClassName);
// Well this is easy!
dynMyClass.GetType().GetMethod(FunctionName).Invoke(dynMyClass,para);
}
// This is a sample class
class MyClass
{
public MyClass(){}
public void GoodMorning(string name)
{
MessageBox.Show(“Good Morning ” + name + “!”);
}
public void OhaioGozaimasu(string name)
{
MessageBox.Show(name + “, Ohaio Gozaimasu!”);
}
}
|