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

解决方案 »

  1.   

    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();
                }        }
      

  2.   

    如果不是同一个dll的form,这里   Type typForm = ass.GetType( "testDLL."+frm );如何改,谢谢!