同创建和使用泛型类略有不同,通过一个中间类的继承,来达到可以在Vs2010上直接编辑泛型窗体.
步骤如下:
1、创建一个类BaseForm继承自Form,创建泛型T,然后可以加上限制.UI包含了界面上的button,不要用Design.cs,不然编译不过去. 2、创建一个中间件,这是一个类继承自BaseForm,它也不能有.design.cs文件.一定要带一个空的构造函数,不然后面的几面无法进入界面设计器. 3、创建一个Form,它继承自StringMiddleware 4、如果想创建一个Int32类型的界面怎么办呢?跳转到第二步,第三不.Int32Form 和StringForm的一些通用方法可以在BaseForm里面进行编写.
5、运行结果:Demo下载:http://download.csdn.net/detail/ruanwei1987/4834043

解决方案 »

  1.   


    我是用到了,就是泛型Usercontrol. 也说不来具体情形
      

  2.   

    这个方法就是让设计器能直接编辑StringForm 或 Int32Form窗体如果不加入StringMiddleware  那没办法在设计器上进行修改窗体.the problem is that the designer is always creating an instance of the base class - direct parent in the inheritance hierarchy. I'm assuming that's because you are changing the definition of the class in design time. So when you are designing MyEditorDialog, the designer actually has an instance of the System.Windows.Forms.Form class on which you add controls. But when you try to design MySubclassedDialog,the designer tries to create instance of the generic class MyEditorDialog<T> and fails. You get the same thing when you declare base form as abstract. The direct descendant can not be designed. The only solution (that I know of) is to add a third concrete, non-generic class in between, which will give your descendant form capability to be designed. Really simple, though you get an extra class that has no other reason to exist.这是MSDN上的Vladimir.Ilic对关于泛型的解释