mardi 4 février 2020

Create instance of generic class from a dynamic type

Is it possible to create a instance of generic class whose type is derived dynamically.

Class GenericClass<T> {

}

Main()
{
    string inputtype = "classname, assemblyname";

    Type type = Type.GetType(inputtype);

    dynamic instance = Activator.CreateInstance(type);

    // Want to create a object of GenericClass<T> but I get compiletime errors.
    // Is it possible to do like below?

    GenericClass<instance> = new GenericClass<instance>();
}




Aucun commentaire:

Enregistrer un commentaire