我有一个 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")
                            {
                                //ControlCollection cc = pi.PropertyType;
                            }
                        }
                    }
                }
到这里迷茫了,不知道怎么获取或转化 对象已获取 对象的属性!
求指点!
C#反射 解析exe

解决方案 »

  1.   

    pis不就是属性集合吗,你就遍历它取其中各个属性的name,value等信息就行了
      

  2.   

    不是的也!版主大大! 这里的属性 的意思有点不同于 控件的属性 指的的是 类的属性 类里面包含 很多自己的属性 方法 对象
    if (pi.PropertyType.Name == "ControlCollection")
    {
       //ControlCollection cc = pi.PropertyType;
    }
    看我这里就是 去找 属性名为 ControlCollection 的对象 想通过这个对象便利出 所有的控件!
    貌似 转化不了! 
    或者是我没理解过来,大大贴代码 嘿嘿!
      

  3.   

    不是的也!版主大大! 这里的属性 的意思有点不同于 控件的属性 指的的是 类的属性 类里面包含 很多自己的属性 方法 对象
     if (pi.PropertyType.Name == "ControlCollection")
     {
        //ControlCollection cc = pi.PropertyType;
     }
     看我这里就是 去找 属性名为 ControlCollection 的对象 想通过这个对象便利出 所有的控件!
     貌似 转化不了! 
     或者是我没理解过来,大大贴代码 嘿嘿! 
      

  4.   

    这种方式的反射我没试过,不知道。但我知道用API是可以达到你的要求
      

  5.   

    哥们儿!api可以 ,到是怎么个可以法啊
      

  6.   


            StringBuilder stringBuilder = new StringBuilder();
            private void button1_Click(object sender, EventArgs e)
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                Type formType = assembly.GetType("WindowsFormsApplication1.Form1");
                Object formObject = Activator.CreateInstance(formType);
                BuildControlsTree(formType, formObject, 1);
                MessageBox.Show(stringBuilder.ToString());
            }        private void BuildControlsTree(Type type, Object obj, Int32 i)
            {
                var controlsProperty = type.GetProperty("Controls");
                ICollection controls = controlsProperty.GetValue(obj) as ICollection;
                if (controls == null || controls.Count == 0)
                {
                    return;
                }
                foreach (var item in controls)
                {
                    Type controlType = item.GetType();
                    stringBuilder.AppendLine(new String(' ', i) + controlType.Name);
                    BuildControlsTree(controlType, item, (i + 1) * 3);
                }
            }
      

  7.   

    PropertyInfo.GetValue(传入属性的对象);
      

  8.   

    可以在From1 增加一个方法此方法遍历所有控件 并返回集合
    然后通过反射调用此方法
      

  9.   


    Type t =this.GetType();
                FieldInfo txt = t.GetField("TextBox1", BindingFlags.NonPublic | BindingFlags.Instance);
                if (txt != null)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(txt.FieldType);
                    PropertyDescriptor myProperty = properties.Find("Text", false);
                    if (myProperty != null)
                    {
                        Object ctr = txt.GetValue(this);
                        Response.Write(myProperty.GetValue(ctr));
                    }
                }
                else {
                    Response.Write("没找到控件");
                }获取页面上TextBox1.Text值,已测试通过。
      

  10.   

    Thank you! I love you!Object formObject = Activator.CreateInstance(formType);
    关键一句、打通经脉!
      

  11.   

    http://liujb1982.blog.163.com/blog/static/114911572201011162327637/liuxibei1987 找到的一个列子 very good !