Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can visual studio designer show classes inheriting generic types?

I am trying to clean up all designer errors in our solutions and ran into the following 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: DoubleAttributeTextBoxBase --- The base class 'NumericAttributeTextBoxBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

The classes are both defined in the same assembly so I know it's not a reference problem. I'm wondering if it has anything to do with the fact that the base class is generic. Any ideas?

public class DoubleAttributeTextBoxBase : NumericAttributeTextBoxBase<double>

public class NumericAttributeTextBoxBase<T> : TextBox where T : IComparable, IComparable<T>
like image 976
bsh152s Avatar asked Jul 29 '11 18:07

bsh152s


2 Answers

The base class for a class being designed must be non-abstract and non-generic. To make a class that inherits from a generic class designable. The workaround is to insert a trivial non-generic class in-between:

public partial class DoubleAttributeTextBoxBase
    : NumericAttributeTextBoxBaseOfDouble
{
    public DoubleAttributeTextBoxBase()
    {
        InitializeComponent();
    }

    // Now DoubleAttributeTextBoxBase is designable.
}

public class NumericAttributeTextBoxBaseOfDouble
    : NumericAttributeTextBoxBase<double>
{
}

To make this as simple as possible, you can even put the non-generic class in the same file as the class you want to design. Just make sure to put it after the class (as I have done above) because the designer expects the first class in the file to be the one being designed.

like image 53
Rick Sladkey Avatar answered Nov 08 '22 17:11

Rick Sladkey


I don't know of a solution, this has been a severe limitation of Visual studio since C# 2.0 came out. The only thing I can say is to add that control to the page at runtime, then at least you can have your designer back for everything else.

from msdn:

Your component or control cannot be a generic type, which is also called a template type or a parameterized type. The design environment does not support generic types.

http://msdn.microsoft.com/en-us/library/ms171843.aspx

like image 1
Jonathan Henson Avatar answered Nov 08 '22 17:11

Jonathan Henson