I have a string containing the name of a generic class with specified type:
string s = "GenericRouteHandler<MemberHandler>";
How do I instantiate an instance from this string? I know how to instantiate a concrete class using Type.GetType() and then use Activator but I am not sure where to go with a generic class.
Like this typeof(GenericRouteHandler<>).MakeGenericType(typeof(MemberHandler));
Of course if you don't have the types you have to use Type.GetType(string)
to get the type instead of typeof
.
EDIT : Then you have to activate the type Activator.CreateInstance()
or invoke a contructor if know the signature myGenericType.GetConstructor(ctorArgsTypes).Invoke(ctorParams);
; it can be faster if cache the consturctors it can be faster then Activator.CreateInstance()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.