类定义:
namespace DynamicFuncTest
{
class BaseTestClass
{
public virtual string TestFunction01()
{
return "BaseTestClass TestFunction01";
}
public virtual string TestFunction02(string sText)
{
return "BaseTestClass TestFunction02" + sText;
}
}
class TestClass01 : BaseTestClass
{
public override string TestFunction01()
{
return "TestClass01 TestFunction01";
}
public override string TestFunction02(string sText)
{
return "TestClass01 TestFunction02" + sText;
}
}
class TestClass02 : BaseTestClass
{
public override string TestFunction01()
{
return "TestClass02 TestFunction01";
}
public override string TestFunction02(string sText)
{
return "TestClass02 TestFunction02" + sText;
}
}
动态创建并调用:
private void Btn_Execute_Class_Click(object sender, EventArgs e)
{
if (T_Function_Class.Text.Length == 0)
{
MessageBox.Show("Pls choice class.");
return;
}
string sClassName = string.Format("DynamicFuncTest.{0}", T_Function_Class.Text);
BaseTestClass test = (BaseTestClass)Activator.CreateInstance(System.Type.GetType(sClassName));
//T_Result_Class.Text = test.TestFunction01();
T_Result_Class.Text = test.TestFunction02(T_Params_Class.Text);
}
其中T_Function_Class.Text为选中的类名