日期:2014-05-19  浏览次数:20716 次

反射问题
我知道一个字符串变量frm   =   "Form2 ",请问怎样通过frm变量把Form2的窗体打开,谢谢!

------解决方案--------------------
Activator.CreateInstance
------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
string frm = "Form2 ";
for (int i = 0; i < Application.OpenForms.Count; i++)
{
if (Application.OpenForms[i].Name == frm)
{
return;
}
}
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
//object obj=thisDll.CreateInstance( "Form2 ");
Type typForm = ass.GetType( "testDLL. "+frm );
if( typForm == null ) return;
Form frmTest = typForm.InvokeMember( null,BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.NonPublic |BindingFlags.Instance | BindingFlags.CreateInstance,null,null,null ) as Form;

if( frmTest == null ) return;
else
{
frmTest.Name=frm;
frmTest.Show();
}


}