Creating an instance of variable with reflection
问题 I want to create an instance of type t with reflection, that is Type t = typeof(string); string s = (t)Activator.CreateInstance(t); // this fails because of convertion string s = Activator.CreateInstance(t) as t // also fails Is there a way to perform such a convertion? Thanks. 回答1: Yes. You have to convert to string , not to t . You may want a generic method, alternatively: public T GetInstance<T>() { Type t = typeof(T); T s = (T)Activator.CreateIstance(t); return s; } As things stand you