jeudi 25 avril 2019

Convert a generic type constructor to non generic type constructor

For example, I have class

class TestClass<T>
{
}

I have some method that receives the constructor of this class. I want to invoke that constructor with some generic parameter without calling MakeGenericType and GetConstructor method again.

static void Main()
{
    var ctor = typeof (TestClass<>).GetConstructors().First();
    Invoke(ctor );
}

static void Invoke(ConstructorInfo ctor)
{
    //ctor.Invoke() - it will not work because TestClass has a generic parameter.
    // I want something like ctor.MakeGenericMethod(typeof(int)).Invoke();
}

Is it possible somehow? Thanks!





Aucun commentaire:

Enregistrer un commentaire