您的位置:首页 > 编程语言 > C#

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());

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐