传进去是什么就创建什么,怎么写呢? 帮帮我吧,好像跟泛型有关
class Class1: Control
{
//private Button btn;
//private Type ItemClass;
public void CreateItems<T>(int count, ref T ItemClass)
{
//Button<t> = new Button<t>();
//Type tp = Type.GetType(classname);
Button item = new Button();
item.Left = x;
item.Top = y;
item.Width = 65;
item.Height = 65;
item.Click += new EventHandler(Item_Click);
this.Controls.Add(item);
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 aa = new Class1();
aa.Left = 0;
aa.Top = 0;
aa.Width = 200;
aa.Height = 200;
this.Controls.Add(aa);
//aa.CreateItems(10, ButtonClass);
aa.CreateItems<Button>(10, typeof(Button));
}
有个类class1, 它有个方法createitems, 我希望传进去是button类的,就创建出button类,如果传进去是Panel类的,就创建出Panel类. 传进去的类不只这两种,可以有很多. class1不知道.
可以教教我怎么写吗? 最好是可以运行的代码.
------解决方案--------------------
public void CreateItems<T>(int count) where T : Control,new()
{
int x, y;
x = 0;
y = 0;
for (int i = 0; i < count; i++)