通用表单和VS设计器

我有一个基类

internal partial class View : UserControl where T : class { protected T t; } 

我想从View派生一个孩子

 internal partial class ViewChild : View where T : class { } 

它工作正常,但我无法在VS设计器中编辑ViewChild。 我知道问题是通用基类。 但是我不明白在这种情况下我怎么能避免这种情况。 有什么方法可以解决这个问题吗?

还有另一种方法,它不依赖于编译器标志:

http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/

我真的不建议使用条件编译。 使用框架更好,而不是反对它。

基本上,您可以通过现有框架为VS提供不同的类。 使用TypeDescriptionProvider属性装饰基类,该属性告诉VS使用不同的类作为设计器。

正如原始博客文章中所提到的,可能存在与此变通方法相关的警告,但我在一个项目中整齐地工作,其中> 25个UserControlsinheritance自公共基类。

generics打破了设计者,因为它不能在没有类型T情况下实例化类。 我在我的博客文章中解释了一个解决方法:

http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html

简而言之,您需要使用中间类“解析”类型:

  • BaseControl : UserControl
  • CustomerControl_Design : BaseControl
  • CustomerControl : CustomerControl_Design

然后,您可以根据DEBUGRELEASE编译器开关有条件地将此类从代码中切换出来:

 #if DEBUG namespace MyNamespace { using System; public partial class CustomerEditorControl_Design : BaseEditorControl { public CustomerEditorControl_Design() : base() { InitializeComponent(); } } } #endif public partial class CustomerEditorControl #if DEBUG : CustomerEditorControl_Design #else : BaseEditorControl #endif { } 

这将允许您打开CustomerControl的派生类,遗憾的是,您永远无法在签名中设计带有generics的UI控件。 我的解决方案只是启用派生项的设计。

我不知道为什么CustomerControl : BaseControl将无法工作,因为在这种情况下定义了类型T ,但它根本没有 – 我猜测是因为通用用法的规则。

为了他们的辩护,微软确实说这不受支持。