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#?
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.
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With