Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Type of a nested Generic class in a Generic class in C#

Tags:

c#

generics

In my project I use the following class: Filter<T>.Checker<U> which also has the interface IChecker. It looks like this:

class Filter<T> {

    public interface IChecker {
        ...
    }

    public class Checker<U> : IChecker {
        ...
    }

    List<IChecker> checkers;

    ...

}

The Filter class filters objects of type T. The filter uses the IChecker list to check different fields in the class T in which U is the type of this field in T.

In some other method in a different class I want to create an instance of a checker. In that method the type of T is Transaction, which is know at compile time. The type of U is only known by a Type instance. The code below show how you would normally create an instance of generic class knowing the Type.

Type type = typeof(MyObject<>).MakeGenericType(objectType);
object myObject = Activator.CreateInstance(type);

I want to take this a little bit further and do the following:

Type type = typeof(Filter<Transaction>.Checker<>).MakeGenericType(objectType);
object myObject = Activator.CreateInstance(type);

The part typeof(Filter<Transaction>.Checker<>) doesn't compile. The compiler says: Unexpected use of an unbounded generic name.

Is it possible get the type of a nested Generic class in a Generic class in C#?

like image 538
Simon Karman Avatar asked Jan 02 '17 11:01

Simon Karman


People also ask

How do you find the type of generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

Where is generic type constraint?

The where clause in a generic definition specifies constraints on the types that are used as arguments for type parameters in a generic type, method, delegate, or local function. Constraints can specify interfaces, base classes, or require a generic type to be a reference, value, or unmanaged type.

What is generic type arguments?

The generic argument list is a comma-separated list of type arguments. A type argument is the name of an actual concrete type that replaces a corresponding type parameter in the generic parameter clause of a generic type. The result is a specialized version of that generic type.

What is the difference between a generic class and a generic method?

A generic class or structure can contain nongeneric procedures, and a nongeneric class, structure, or module can contain generic procedures. A generic procedure can use its type parameters in its normal parameter list, in its return type if it has one, and in its procedure code.


2 Answers

Well generics require you to specifiy all or none generic arguments. Since you don't know the second argument at compile time, you have to pass both of them as arguments to MakeGenericType:

Type type = typeof(Filter<>.Checker<>).MakeGenericType(typeof(Transaction), objectType);
object myObject = Activator.CreateInstance(type);

Although you know the type Transaction at compile time, you need to specify via typeof(), but that shouldn't hurt.

I verified via is operator that the type arguments are applied in the expected order.

like image 95
René Vogt Avatar answered Oct 07 '22 14:10

René Vogt


I think you have to omit the first generic parameter T here and pass both types as an array:

Type type = typeof(Filter<>.Checker<>).MakeGenericType(typeof(Transaction),objectType);
like image 24
Stuart Avatar answered Oct 07 '22 13:10

Stuart