10

I've got the following Generic usercontrol declared:

public partial class MessageBase<T> : UserControl
    {
        protected T myEntry;
        public MessageBase()
        {
            InitializeComponent();
        }
        public MessageBase(T newEntry)
        {
            InitializeComponent();
            myEntry = newEntry;
        }    
    }
}

But the compiler won't allow me to do this:

public partial class MessageControl : MessageBase<Post>
{
    public MessageControl()
    {
        InitializeComponent();
    }
}

How do I create a generic user control in C#?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Roberto Bonini
  • 7,164
  • 6
  • 39
  • 47

4 Answers4

16

Try this

public partial class MessageControl : MessageControlBase
{    
    public MessageControl()    
    {
        InitializeComponent();    
    }
}

public class MessageControlBase : MessageBase<Post>
{}

The key to getting the designer to work is that the base class of the class you are editing must not be generic.

rnwood
  • 1,480
  • 10
  • 12
  • 1
    Yes, the designer is a 'stupid' instantiator. It can only instantiate concrete classes - non-abstract, non-generic. This is true for both the base class and any controls / components that are on the control you are editing. Hopefully, they'll fix it someday... – configurator Dec 28 '08 at 09:31
2

Yes. its possible. See Below link to get an idea.

https://web.archive.org/web/20130603104810/http://www.hackersbasement.com/csharp/post/2009/08/29/Generic-Control-Builder-in-ASPNet.aspx

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
dfddf
  • 21
  • 1
1

For one, althought generic controls are possible under .NET, the Visual Studio designers do not support them, so you're on your own if you want to use them. You'll have to instantiate them yourself in your code and perform layout too.

As for the error you mention, it sounds to me like you're looking in the wrong direction. Perhaps you could write the whole error text here?

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

The .NET framework supports them, but as Vilx mantions, the designers simply do not - and any of the xml/markup-based platforms (xaml (wpf) or ASP.NET) will not like generics at all. So the best advice is: don't use them.

A common compromise is to have a Type property (or an object template-property upon which you call GetType()), and simply cast etc inside the control. Likewise, such usage will commonly make use of things like Activator.CreateInstance, and TypeDescriptor (for metadata lookup etc).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I guess I don't recall using the design view on a strongly typed ViewPage in ASP.NET MVC. Does (or how does) the designer handle these? – tvanfosson Dec 27 '08 at 20:05
  • Thanks. But its a curious oversight. – Roberto Bonini Dec 27 '08 at 20:18
  • See Robert Wood's answer. It is a way to work around that specific problem and still use the designer. Even though it's a workaround, it's much better than using a Type property. – configurator Dec 28 '08 at 09:33