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不知道.
可以教教我怎么写吗? 最好是可以运行的代码.

解决方案 »

  1.   

      public List<T> CreateItems<T>(int count) where T : class, new()
            {
                List<T> list = new List<T>();
                for (int i = 0; i < count; i++)
                    list.Add(new T());
                return list;
            }
    这样吗?
      

  2.   


    public T CreateItems<T>() where T : new()
            {
               return new T();
            }
      

  3.   

    List是必须用的吗?我就想
    createitems(类的类: itemclass)
    {
      itemclass item = new itemclass()
    }这样能实现吗?
      

  4.   

    不是的
    如果是下面这样,感觉没什么意义了
    public T CreateItems<T>() where T : class,new()
            {
               return new T();
            }
      

  5.   

    createitems 返回是void,怎么写?
      

  6.   

        class Class1: Control
        {
            public void CreateItems<T>(int count, ref T)
            {
                int x, y;
                x = 0;
                y = 0;
                for (int i = 0; i < count; i++)
                {
                    T item = new T();
                    (Control)item.Left = x;
                    item.Top = y;
                    item.Width = 65;
                    item.Height = 65;
                    item.Text = i.ToString();
                    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<Button>(10, Button);
            }还是不行啊, 出错部分要怎么改?
      

  7.   


    public void CreateItems<T>(int count) where T : Control,new()
            {
                int x, y;
                x = 0;
                y = 0;
                for (int i = 0; i < count; i++)
                {
                    T item = new T();
                    item.Left = x;
                    item.Top = y;
                    item.Width = 65;
                    item.Height = 65;
                    item.Text = i.ToString();
                    item.Click += new EventHandler(Item_Click);
                    this.Controls.Add(item);
                }
            }
      

  8.   

    你想做什么?你把T对象传递进去,又在里面创建T对象?这不显得多余吗?到底那个T对象是要创建的,还是外部给的,必须明确指出,完全看不出你的意图。
      

  9.   

     class Class1 : Control
        {
            private Dictionary<string, Control> Controls = new Dictionary<string, Control>();
            public void CreateItems<T>(int count) where T : Control, new()
            {
                string str = Regex.Match(typeof(T).ToString(), @"(?<=\.)\w+$").Value;
                int n = 0;
                for (int i = 0; n < count; i++)
                {
                    if (!Controls.ContainsKey(str + i) )
                    {
                        T item = new T();
                        item.Text = str + i;
                        Controls.Add(str + i, item);
                        n++;
                    }            }
            }
            public Control this[string key]
            {
                get { return Controls[key]; }
            }
        }
      

  10.   

    传到里面的是类的类,我也不知道怎么说了才清楚,别骂我
    hjywyj和superliu1122好像是写对了,
    public void CreateItems<T>(int count) where T : Control, new()
    不过这个过程调用要怎么写?写全一点吧,我是新手
      

  11.   

    有个容器,我告诉它你创建5个button,它就创建5个button,我告诉它你创建10个textbox,它就创建10个textbox.
      

  12.   

    aa.CreateItems<TextBox>(10);
    搞定,结贴