Created
May 30, 2012 13:34
-
-
Save brianium/2836357 to your computer and use it in GitHub Desktop.
Creating Dynamic Generic Types in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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