找到当前项目中的所有窗体的控件

 List<Form> names = new List<Form>();
            List<string> GridList = new List<string>();
            foreach (var type in this.GetType().Assembly.GetTypes())
            {
                if (typeof(Form).IsAssignableFrom(type))
                {
                    try
                    {
                        using (Form form = Activator.CreateInstance(type) as Form)
                        {
                            names.Add(form); //names.Add(form.name)
                        }
                    }
                    catch
                    {
                        Console.WriteLine(string.Format("类型为 {0} 的窗体没有无参的构造函数,无法获取其名称", type));
                    }
                }
            }

            foreach (Form item in names)
            { 
                foreach (var field in item.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                {
                    if (field.FieldType == typeof(GridControl))
                    {
                            GridList.Add(field.Name); //names.Add(form.name)
                    }
                }
            }
            string s = "";
            foreach (var item in GridList)
            {
                s += item+",";
               
            }
            MessageBox.Show(s);

 第二种方法

            string path = Application.StartupPath + "\\layout";
            List<Form> names = new List<Form>();
            List<string> GridList = new List<string>();
            foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes())
            {
                if (typeof(Form).IsAssignableFrom(type))
                {
                    try
                    {
                        using (Form form = Activator.CreateInstance(type) as Form)
                        {
                            names.Add(form); //names.Add(form.name)
                        }
                    }
                    catch
                    {
                        Console.WriteLine(string.Format("类型为 {0} 的窗体没有无参的构造函数,无法获取其名称", type));
                    }
                }
            }
            foreach (Form item in names)
            {
                foreach (var field in item.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
                {
                    if (field.FieldType == typeof(GridControl))
                    {
                        //GridList.Add(field.Name); //names.Add(form.name)
                        GridControl Grid = new GridControl();
                        Grid.Name = field.Name;
                        try
                        {
                            Grid.MainView.RestoreLayoutFromXml(path + "\\" + item.Name + "窗体的" + field.Name);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

 

找到当前项目中的所有窗体的控件

上一篇:Window10右键转圈卡顿,win+e、任务栏打开文件管理器卡顿,单击文件或文件夹卡顿问题解决


下一篇:win32- GetMessage的使用