日期:2014-05-17  浏览次数:20851 次

C# 根据 一个画面的exe 解析出 里面所有的控件
我有一个 Winfrom 程序 有 From1 里面若干 控件!编译生成exe,
然后再建一个Winfrom 程序 读取 上一个 生成好的exe 将里面 用到的 所有的控件和其某些属性 列出显示!
(也可以只列出指定类型的控件 比如 TextBox Commbox Grid)
效果举例:
类型        名称           可见          可改
TextBox   txt_Name     true        false
TextBox   txt_Age      true        true
Commbox   cbx_Country  true        true
...       ...          ...         ...

看了下反射
 OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "|*.exe";
            DialogResult dr = fd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                this.textBox1.Text = fd.FileName;

                //加载exe
                dllexe = Assembly.LoadFrom(fd.FileName);
                //获取所有模块
                Module[] modules = dllexe.GetModules();
                //筛选Form模块
                Type[] types = modules[0].GetTypes();


                int i = 1;
                foreach (Type t in types)
                {
                    if (t.BaseType == typeof(Form))
                    {
                        PropertyInfo [] pis = t.GetProperties();
                        foreach (PropertyInfo pi in pis)
                        {
                            if (pi.PropertyType.Name == "ControlCollection")