Can visual studio designer show classes inheriting generic types?
Asked Answered
F

2

5

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>
Fount answered 29/7, 2011 at 18:9 Comment(1)
I know this may seem silly but have you checked to make sure they are in the same namespace? Does the application compile?Durware
D
9

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.

Dime answered 29/7, 2011 at 19:36 Comment(1)
Nongeneric base class needs to have parameterless constructor, doesn't work for me without it. It needs rebuilding after the constructor is introduced.Goodsized
D
1

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

Durware answered 29/7, 2011 at 18:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.