Skip to content

Instantly share code, notes, and snippets.

@brianium
Created May 30, 2012 13:34
Show Gist options
  • Save brianium/2836357 to your computer and use it in GitHub Desktop.
Save brianium/2836357 to your computer and use it in GitHub Desktop.
Creating Dynamic Generic Types in C#
void Main()
{
var genericType = typeof(GenericClass<>);
Type[] t = { typeof(int) };
var toCreate = genericType.MakeGenericType(t);
object o = Activator.CreateInstance(toCreate, "brian", "scaturro");
o.Dump();
}
class GenericClass<T>
{
public string Prop1 {get;set;}
public string Prop2 {get;set;}
public GenericClass(string prop1, string prop2)
{
Prop1 = prop1;
Prop2 = prop2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment