views:

763

answers:

4

Hi all, I've got a control that's declared thus:

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

EDIT; Removed the interface.

The T stands for the information I'm going to be displaying within the control. The complier appears to like this.

But it wont allow me to do this:

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

EDIT: I can't reproduce the error after starting from sratch so maybe was something else

Any ideas on how to do this????

Thanks in advance

+1  A: 

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-
A: 

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
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
Thanks. But its a curious oversight.
Roberto Bonini
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
+7  A: 

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.

Robert Wood
Thanks. I'll try this.
Roberto Bonini
Cool workaround :)
leppie
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
It works!!! Thanks a lot.
Roberto Bonini
+2  A: 

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

http://www.hackersbasement.com/csharp/post/2009/08/29/Generic-Control-Builder-in-ASPNet.aspx

dfddf