当前位置:编程学习 > C#/ASP.NET >>

不知对君胃口不,泛型之Form

同创建和使用泛型类略有不同,通过一个中间类的继承,来达到可以在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 --------------------编程问答-------------------- 基于什么业务逻辑下会用到呢? --------------------编程问答--------------------
我是用到了,就是泛型Usercontrol. 

也说不来具体情形
--------------------编程问答-------------------- 顶起 --------------------编程问答--------------------
LZ对泛型有深刻体会啊 --------------------编程问答-------------------- 支持下分享,但有什么用途呢? --------------------编程问答-------------------- 这样做法论坛里面有人做过,问题是设计器支持的不好,尤其是当你使用窗体继承的时候。

我想用它做一些特定的窗体会有些用途,但是不是一个通用的推荐的方式。 --------------------编程问答-------------------- 这个方法就是让设计器能直接编辑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对关于泛型的解释
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,