在运行时任意指定对象的创建类型,甚至是用表示类型的名字的字符串创建所需的对象,.net Framwork的反射机制给我们带来了解决问题的方法。这里,若只需要创建一般的对象,我们可以通过System.Activator来实现,而较复杂的我们可以通过获取构造方法来实现。
反射Reflection是.net中重要机制,通过反射,可以在运行时获得.net中每一个类型(包括类、结构、委派、接口、枚举)的成员,包括方法、属性、事件以及构造函数等,还可以获得每个成员的名称、限定符和参数等,有了反射,就可以对每一个类型了如指掌。如果获得了构造函数的信息,就可以直接创建对象,即使这个对象的类型在编译的时候还不知道。
1/// <summary>
2
3 /// CreateNewControls 根据空间的名称,类型字符串,大小、位置去动态的生成一个控件
4
5 /// </summary>
6
7 /// <param name="targetControl"> 控件加载到的容器</param>
8
9 /// <param name="ctlName">生成的控件实例名称</param>
10
11 /// <param name="ctlType">生成的控件类型字符串如(TextBox、Button等)</param>
12
13 /// <param name="ctlSize">控件的大小</param>
14
15 /// <param name="ctlLocation">控件的位置</param>
16
17 /// <returns>生成的控件实例</returns>
18
19 private Control CreateNewControls(Control.ControlCollection targetControl,string ctlName,Type ctlType, System.Drawing.Size ctlSize,System.Drawing.Point ctlLocation)
20
21 {
22
23 Control toCreate;
24
25 toCreate = (Control)System.Activator.CreateInstance(ctlType);
26
27 toCreate.Name = ctlName;
28
29 toCreate.Size = ctlSize;
30
31 toCreate.Location = ctlLocation;
32
33 targetControl.Add(toCreate);
34
35 return toCreate;
36
37 }
38
39 Size cbSize = new Size(160,40);
40
41 Point cbPoint = new Point(64,206);
42
43 Control c1 = CreateNewControls(this.Controls,"control1",Type.GetType("System.Windows.Forms.CheckBox, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),cbSize,cbPoint);
44
45 c1.Text =" Check Box";
46
47 .ne tFramework 1.1上,Type.GetType("System.Windows.Forms.CheckBox, System.Windows.Forms,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089")。
48
49
2
3 /// CreateNewControls 根据空间的名称,类型字符串,大小、位置去动态的生成一个控件
4
5 /// </summary>
6
7 /// <param name="targetControl"> 控件加载到的容器</param>
8
9 /// <param name="ctlName">生成的控件实例名称</param>
10
11 /// <param name="ctlType">生成的控件类型字符串如(TextBox、Button等)</param>
12
13 /// <param name="ctlSize">控件的大小</param>
14
15 /// <param name="ctlLocation">控件的位置</param>
16
17 /// <returns>生成的控件实例</returns>
18
19 private Control CreateNewControls(Control.ControlCollection targetControl,string ctlName,Type ctlType, System.Drawing.Size ctlSize,System.Drawing.Point ctlLocation)
20
21 {
22
23 Control toCreate;
24
25 toCreate = (Control)System.Activator.CreateInstance(ctlType);
26
27 toCreate.Name = ctlName;
28
29 toCreate.Size = ctlSize;
30
31 toCreate.Location = ctlLocation;
32
33 targetControl.Add(toCreate);
34
35 return toCreate;
36
37 }
38
39 Size cbSize = new Size(160,40);
40
41 Point cbPoint = new Point(64,206);
42
43 Control c1 = CreateNewControls(this.Controls,"control1",Type.GetType("System.Windows.Forms.CheckBox, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"),cbSize,cbPoint);
44
45 c1.Text =" Check Box";
46
47 .ne tFramework 1.1上,Type.GetType("System.Windows.Forms.CheckBox, System.Windows.Forms,Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089")。
48
49
我们如何取得所用Windows.Form程序集的版本和强名称?可以用GetType(CheckBox).AssemblyQualifiedName这样的语法,一旦得到了这些信息,我们就可以将这些信息用于其它任何控件,因为他们都来自于同一个版本Windows.Forms程序集。
(来源:CSDN)
本人测试实例:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
int i = 1;
//动态增加TextBox控件
private void button1_Click(object sender, EventArgs e)
{
try
{
Form2 frm = new Form2();
bool flag = false;
string controlName = "";
while (!flag)
{
bool flag2 = false;
controlName = "txtServicerAdd" + i.ToString();
//判断是否存在控件
foreach (Control control in frm.Controls)
{
if (control.Name == controlName)
{
flag2 = true;
}
}
if (!flag2) //控件不存在,退出循环
{
flag = true;
}
else
{
i++;
}
}
Size txtSize = new Size(100, 21);
Point txtPoint = new Point((180 + 110 * (i - 1)), 31);
//取得所用Windows.Form程序集的版本和强名称
TextBox cb = new TextBox();
string cbAssemblyQualifiedName = cb.GetType().AssemblyQualifiedName;
//动态生成TextBox
CreateNewControl(this.Controls, controlName, Type.GetType(cbAssemblyQualifiedName),
txtSize, txtPoint);
////另外一种方法
//TextBox txt = new TextBox();
//CreateNewControl(this.Controls, controlName, txt.GetType(),txtSize, txtPoint);
button1.Location = new Point((180 + 110 * i), 31);
i++;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// CreateNewControls 根据空间的名称,类型字符串,大小、位置去动态的生成一个控件
/// </summary>
/// <param name="targetControl">控件加载到的容器</param>
/// <param name="ctlName">生成的控件实例名称</param>
/// <param name="ctlType">生成的控件类型字符串如(TextBox、Button等)</param>
/// <param name="ctlSize">控件的大小</param>
/// <param name="ctlLocation">控件的位置</param>
private void CreateNewControl(Control.ControlCollection targetControl, string ctlName,
Type ctlType,System.Drawing.Size ctlSize, System.Drawing.Point ctlLocation)
{
Control toCreate;
toCreate = (Control)System.Activator.CreateInstance(ctlType);
toCreate.Name = ctlName;
toCreate.Size = ctlSize;
toCreate.Location = ctlLocation;
targetControl.Add(toCreate);
}
}
{
public Form2()
{
InitializeComponent();
}
int i = 1;
//动态增加TextBox控件
private void button1_Click(object sender, EventArgs e)
{
try
{
Form2 frm = new Form2();
bool flag = false;
string controlName = "";
while (!flag)
{
bool flag2 = false;
controlName = "txtServicerAdd" + i.ToString();
//判断是否存在控件
foreach (Control control in frm.Controls)
{
if (control.Name == controlName)
{
flag2 = true;
}
}
if (!flag2) //控件不存在,退出循环
{
flag = true;
}
else
{
i++;
}
}
Size txtSize = new Size(100, 21);
Point txtPoint = new Point((180 + 110 * (i - 1)), 31);
//取得所用Windows.Form程序集的版本和强名称
TextBox cb = new TextBox();
string cbAssemblyQualifiedName = cb.GetType().AssemblyQualifiedName;
//动态生成TextBox
CreateNewControl(this.Controls, controlName, Type.GetType(cbAssemblyQualifiedName),
txtSize, txtPoint);
////另外一种方法
//TextBox txt = new TextBox();
//CreateNewControl(this.Controls, controlName, txt.GetType(),txtSize, txtPoint);
button1.Location = new Point((180 + 110 * i), 31);
i++;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// CreateNewControls 根据空间的名称,类型字符串,大小、位置去动态的生成一个控件
/// </summary>
/// <param name="targetControl">控件加载到的容器</param>
/// <param name="ctlName">生成的控件实例名称</param>
/// <param name="ctlType">生成的控件类型字符串如(TextBox、Button等)</param>
/// <param name="ctlSize">控件的大小</param>
/// <param name="ctlLocation">控件的位置</param>
private void CreateNewControl(Control.ControlCollection targetControl, string ctlName,
Type ctlType,System.Drawing.Size ctlSize, System.Drawing.Point ctlLocation)
{
Control toCreate;
toCreate = (Control)System.Activator.CreateInstance(ctlType);
toCreate.Name = ctlName;
toCreate.Size = ctlSize;
toCreate.Location = ctlLocation;
targetControl.Add(toCreate);
}
}