How to use Activator.CreateInstance to instantiate a generic type based on a name and type parameters? It's not quite as simple as I thought. First mark the fully qualified name of the class by appending a backquote (`) and the number of type arguments. Then you can do make the type generic by calling the MakeGenericType method and passing in the types.
string generic = string.Format("{0}`{1}", _className, _genericTypeParams.Count);
Type genericType = Type.GetType(generic).MakeGenericType(_genericTypeParams.ToArray());
x = Activator.CreateInstance(genericType);
Voila
Wednesday, November 12, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment