Skip to content

Instantly share code, notes, and snippets.

@nbxx
Created September 17, 2016 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbxx/69e6d0d04d9be6fe7d884439361b74be to your computer and use it in GitHub Desktop.
Save nbxx/69e6d0d04d9be6fe7d884439361b74be to your computer and use it in GitHub Desktop.
public static object CreateInstanceGeneric(Type source, Type[] typeArguments, params object[] args)
{
object result = null;
Type genericType = source.MakeGenericType(typeArguments);
try
{
result = Activator.CreateInstance(genericType, args);
}
catch
{
result = FormatterServices.GetUninitializedObject(genericType);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment