Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create constructor for generic class using reflection

I want to create a class using reflection for a generic class.
Could someone tell me how to create it?

I have

public class SomeClass<T>
{
  ....
}

I need to create a class for SomeClass<T> using reflection.

like image 217
VIRA Avatar asked Dec 04 '11 16:12

VIRA


1 Answers

Use MakeGenericType method:

Type myParameterizedSomeClass = typeof(SomeClass<>).MakeGenericType(typeof(MyParameter));
ConstructorInfo constr = myParameterizedSomeClass.GetConstructor(typeof(ConstrParamType1),typeof(ConstrParamType2));
like image 185
Sergey Kalinichenko Avatar answered Sep 30 '22 10:09

Sergey Kalinichenko