Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType on generic types

I'm trying register presenters with Windsor using the convention based method but trying to do this in VB.NET, but the problem is it does not want to compile this statement:

Dim type = GetType(AbstractPresenter(Of))

I am getting : Too few type arguments to AbstractPresenter(Of TView, TPresenter)

Which I don't understand because this is a valid statement according to question. Also showing valid in other C# to VB.NET converters when converting typeof(AbstractPresenter<>).

Any ideas?

like image 351
adriaanp Avatar asked Dec 02 '10 10:12

adriaanp


People also ask

Can generic classes be constrained?

You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.

What is generic data type?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

Which types can be used as arguments of a generic type?

The actual type arguments of a generic type are. reference types, wildcards, or. parameterized types (i.e. instantiations of other generic types).


1 Answers

There are two type arguments, and you need to specify this, just as you would do for multi-dimensional arrays:

Dim type = GetType(AbstractPresenter(Of ,))

Looks weird, but now the compiler knows that AbstractPresenter expects two type arguments.

By the way, C# has the same requirement. So the above would be written as:

var type = typeof(AbstractPresenter<,>);
like image 159
Konrad Rudolph Avatar answered Oct 29 '22 05:10

Konrad Rudolph