我有一个 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
然后再建一个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
解决方案 »
- 正交表???
- 如何让一个窗体的自定义属性在别的窗体引用时只读?
- datagridview rows[0] 怎么无法隐藏啊。
- c#中用Webclient类能否遍历文件夹下的所有文件,依次读取呢??
- 请做过网络获取的编程者帮忙用C#写一份登陆到www.hexun.com的代码
- C#编应用程序时FindControl的问题
- 大伙都用什么工具建模,对c#来说好使一些的?
- 点击按钮弹出剧中遮罩层????(点击后不显示)在线等~~~~~
- 如何实现让textBox控件自动向下滚动
- 原来www.devhood.com这个网站也不错!
- ArrayList的count最大能为多少?
- WPF Combobox里面的选项是checkbox
if (pi.PropertyType.Name == "ControlCollection")
{
//ControlCollection cc = pi.PropertyType;
}
看我这里就是 去找 属性名为 ControlCollection 的对象 想通过这个对象便利出 所有的控件!
貌似 转化不了!
或者是我没理解过来,大大贴代码 嘿嘿!
if (pi.PropertyType.Name == "ControlCollection")
{
//ControlCollection cc = pi.PropertyType;
}
看我这里就是 去找 属性名为 ControlCollection 的对象 想通过这个对象便利出 所有的控件!
貌似 转化不了!
或者是我没理解过来,大大贴代码 嘿嘿!
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);
}
}
然后通过反射调用此方法
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值,已测试通过。
关键一句、打通经脉!