C#读取其他运行的Exe上面的控件类型集合
2014-01-20 21:27
351 查看
button1为一个按钮
private void button1_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "|*.exe";
DialogResult dr = fd.ShowDialog();
if (dr == DialogResult.OK)
{
//加载exe
Assembly dllexe = Assembly.LoadFrom(fd.FileName);
//获取所有模块
Module[] modules = dllexe.GetModules();
//筛选Form模块
for (int i = 0; i < modules.Length; i++)
{
Type[] types = modules[i].GetTypes();
for (int j = 0; j < types.Length; j++)
{
//如果类型为Form
if (types[j].BaseType == typeof(Form))
{
//遍历Form上面的所有控件
PropertyInfo pi = types[j].GetProperty("Controls");
ICollection controls = pi.GetValue(Activator.CreateInstance(types[j]), null) as ICollection;
if (controls == null || controls.Count == 0)
{
return;
}
else
{
foreach (Control item in controls)
{
Type controlType = item.GetType();
str.AppendLine(controlType.Name.ToString());//所获得的所有控件名称
}
}
}
}
}
}
MessageBox.Show( str.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder str = new StringBuilder();
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "|*.exe";
DialogResult dr = fd.ShowDialog();
if (dr == DialogResult.OK)
{
//加载exe
Assembly dllexe = Assembly.LoadFrom(fd.FileName);
//获取所有模块
Module[] modules = dllexe.GetModules();
//筛选Form模块
for (int i = 0; i < modules.Length; i++)
{
Type[] types = modules[i].GetTypes();
for (int j = 0; j < types.Length; j++)
{
//如果类型为Form
if (types[j].BaseType == typeof(Form))
{
//遍历Form上面的所有控件
PropertyInfo pi = types[j].GetProperty("Controls");
ICollection controls = pi.GetValue(Activator.CreateInstance(types[j]), null) as ICollection;
if (controls == null || controls.Count == 0)
{
return;
}
else
{
foreach (Control item in controls)
{
Type controlType = item.GetType();
str.AppendLine(controlType.Name.ToString());//所获得的所有控件名称
}
}
}
}
}
}
MessageBox.Show( str.ToString());
}
相关文章推荐
- C#中将Object数组转换为String(或者其他类型)的数组(集合)
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- C# 引用其他命名空间的公共变量时的警告:由于"***"是引用封送类的字段,访问上面的成员可能导致运行时异常
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- C# 尝试读取或写入受保护的内存,这通常指示其他内存已损坏。错误类型为:System.AccessViolationException。
- 如果你也会C#,那不妨了解下F#(3):F#集合类型和其他核心类型
- C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- C#(也适用其他)的初学者对string是值类型还是引用类型搞不清楚,还有对参数传递也比较迷糊
- C#把datetime类型的日期转化成其他格式方法总结
- 使用用户控件时,网站运行没有问题,但生成网站时却出错:命名空间“ASP”中不存在类型或命名空间名称“ascx_ub_weightbar_ascx” 的解决方法
- C#下使用第三方开源控件读取Excel文件的内容
- 怎样在C#代码的运行中,弹出confirm窗口,然后得到确认的值,然后再向下继续执行其他代码?
- C# 把指定控件显示在最上面
- VS2010编写exe程序,在其他电脑上无法运行,提示缺少mfc100.dll的解决办法
- C#集合类型大揭秘 【转载】
- [c#基础]泛型集合的自定义类型排序
- C#中主窗体Panel中加载其他多个窗体Panel控件
- [C#基础知识]专题十三:全面解析对象集合初始化器、匿名类型和隐式类型
- C#发布的Setup.exe文件如何读取同一目录下的ini文件