问一下,我有一个窗体的类名,类名为字符串,我怎么通过这个类名调用这个窗体?? 

解决方案 »

  1.   

    楼上怎么实现啊????我这样搞的时候
    TYPE type=TYPE,GETTYPE(classname)得到的type为空!!!
      

  2.   

    给一个参考/// <summary>
            /// Open child window
            /// </summary>
            /// <param name="ChildTypeString"></param>
            private void OpenWindow(string ChildTypeString)
            {
                Form myChild = null;
                if (!ContainMDIChild(ChildTypeString))
                {
                    // Get current process assembly
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    // Create data type using type string
                    Type typForm = assembly.GetType(ChildTypeString);
                    // Create object using type's "InvokeMember" method
                    Object obj = typForm.InvokeMember(
                        null,
                        BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Instance | BindingFlags.CreateInstance,
                        null,                    
                        null,
                        null);
                    // Show child form 
                    if (obj != null)
                    {
                        myChild = obj as Form;
                        myChild.MdiParent = this;
                        myChild.Show();
                        myChild.Focus();
                    }
                }
            }        /// <summary>
            /// Search mdi child form by specific type string
            /// </summary>
            /// <param name="ChildTypeString"></param>
            /// <returns></returns>
            private bool ContainMDIChild(string ChildTypeString)
            {
                Form myMDIChild = null;
                foreach (Form f in this.MdiChildren)
                {
                    if (f.GetType().ToString() == ChildTypeString)
                    {
                        // found it 
                        myMDIChild = f;
                        break;
                    }
                }            // Show the exist form
                if (myMDIChild != null)
                {
                    myMDIChild.TopMost = true;
                    myMDIChild.Show();
                    myMDIChild.Focus();
                    return true;
                }
                else
                    return false;
            }调用方法,例如:
    OpenWindow(typeof(form1).ToString());
      

  3.   

    TYPE type=TYPE.GetType(namespace+classname)
    Type.GetType(consoleAppliaction.class1)
      

  4.   

                
                Assembly a = Assembly.LoadFrom("WindowsApplication1.dll");
                //gettype中的字符串应该是fullname(全名)
                Type t = a.GetType("WindowsApplication1.Form2");            object obj = Activator.CreateInstance(t);
                Form f = (Form)obj;
                f.Show();
      

  5.   

    TYPE,GETTYPE(namespace+classname) 
    TYPE,GETTYPE(console.class1)