C#: Non-constructed generics as properties (eg. List<T>)

Posted by Dav on Stack Overflow See other posts from Stack Overflow or by Dav
Published on 2010-06-18T15:27:48Z Indexed on 2010/06/18 15:43 UTC
Read the original article Hit count: 212

Filed under:
|
|
|

The Problem

It's something I came across a while back and was able to work around it somehow. But now it came back, feeding on my curiosity - and I'd love to have a definite answer.

Basically, I have a generic dgv BaseGridView<T> : DataGridView where T : class. Constructed types based on the BaseGridView (such as InvoiceGridView : BaseGridView<Invoice>) are later used in the application to display different business objects using the shared functionality provided by BaseGridView (like virtual mode, buttons, etc.).

It now became necessary to create a user control that references those constructed types to control some of the shared functionality (eg. filtering) from BaseGridView. I was therefore hoping to create a public property on the user control that would enable me to attach it to any BaseGridView in Designer/code: public BaseGridView<T> MyGridView { get; set; }. The trouble is, it doesn't work :-) When compiled, I get the following message:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

Solutions?

I realise I could extract the shared functionality to an interface, mark BaseGridView as implementing that interface, and then refer to the created interface in my uesr control.

But I'm curious if there exists some arcane C# command/syntax that would help me achieve what I want - without polluting my solution with an interface I don't really need :-)

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics