Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do base Windows Forms form class with generic types stop the designer loading?

I am trying to have a base Windows Forms form which holds common functionality and controls - but also holds references to a class which requires a type for its methods. Each form will represent a different type, so I thought that I could do something along the lines of this:

public partial class Base<T> : Form where T : BaseClass
{
    private GenericHandler handler = new GenericHandler();
}

public class BaseClass { }

public class GenericHandler
{
    public void DoSomethingWithInstance<T>(T instance) where T : BaseClass
    {

    }
}

My designer class declaration also mirrors what my form has. Now when I do my second form which represents the type Foo, I can't access the designer, because I get this error:

The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: Foo --- The base class 'WindowsFormsApplication1.Base' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

FooClass --- The base class 'WindowsFormsApplication1.BaseClass' cannot be designed.

public partial class Foo : Base<FooClass>
{
    public Foo()
    {
        InitializeComponent();
    }
}

public class FooClass : BaseClass { }

Why does this happen/what am I doing wrong or are there any other methods to do this?

like image 281
LukeHennerley Avatar asked Nov 12 '12 14:11

LukeHennerley


1 Answers

When a Windows Form or a user UserControl is loaded in the designer, basically the designer is creating an instance of the base class (that class that your custom form or control directly derives from) and then executes the InitializeComponents() method manually/explicitly through reflection to build up the intended design of your control.

In your case however it can not create an instance of the base class, because it has a generic parameter. The same thing happens if the base class of your form or your control is abstract or does not have a default constructor. In those cases the designer will also not be able to create an instance of your base class.

There is a workaround for this using the TypeDescriptionProviderAttribute where you can give the designer a replacement class that it should instantiate instead.

like image 103
bitbonk Avatar answered Sep 24 '22 00:09

bitbonk