I have a base class
internal partial class View : UserControl
where T : class
{
protected T t;
}
and I want to derive a chil
There is another way, and it doesn't rely on compiler flags:
http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/
I really wouldn't advise the use of conditional compilation. Much better to work with the framework, and not against it.
Basically, you can give VS a different class through the existing framework. You decorate your base class with a TypeDescriptionProvider attribute which tells VS to use a different class as a designer.
As mentioned in the original blog post, there may be caveats associated with this workaround, but I got it working neatly on a project with > 25 UserControls inheriting from a common base class.
Generics break the designer because it cannot instantiate the class without a type T
. I explain a workaround in my blog post:
http://adamhouldsworth.blogspot.co.uk/2010/02/winforms-visual-inheritance-limitations.html
In short, you need to "resolve" the type with an intermediary class:
BaseControl<T> : UserControl
CustomerControl_Design : BaseControl<Customer>
CustomerControl : CustomerControl_Design
You can then conditionally switch this class out of the code based on the DEBUG
or RELEASE
compiler switches:
#if DEBUG
namespace MyNamespace
{
using System;
public partial class CustomerEditorControl_Design : BaseEditorControl<Customer>
{
public CustomerEditorControl_Design()
: base()
{
InitializeComponent();
}
}
}
#endif
public partial class CustomerEditorControl
#if DEBUG
: CustomerEditorControl_Design
#else
: BaseEditorControl<Customer>
#endif
{
}
This will let you open the derived class of CustomerControl
, unfortunately you will never be able to design a UI control with generics in the signature. My solution is only enabling the design of derived items.
I have no idea why CustomerControl : BaseControl<Customer>
won't work as in this case the type T
is defined, but it simply doesn't - I'm guessing because of the rules of generic usage.
To their defense, Microsoft do say that this isn't supported.