I need to create instance of template class like this:
Type T = Type.GetType(className).GetMethod(functionName).ReturnType;
var comparer = new MyComparer<T>(); // ERROR: "The type or namespace name 'T' could not be found"
I found this answer where this is possible only with reflection. But using reflection I get object which I need to cast to my generic type. I tried like this
Type myGeneric = typeof(IEqualityComparer<>);
Type constructedClass = myGeneric.MakeGenericType();
object created = Activator.CreateInstance(constructedClass);
var comparer = (T)Convert.ChangeType(created, T);// ERROR: "The type or namespace name 'T' could not be found"
but get the same error. How to solve it?
Aucun commentaire:
Enregistrer un commentaire