Creating a generic container, and inner type, using Reflection

May 26, 2010

Type t = Type.GetType(outer_type);
if (t == null)
srt.LogClient.Log(Tools.LogLevel.Error, "Failed to instantiate type {0}", outer_type);
if (t.IsGenericType == true)
Type[] innerTArr = t.GetGenericArguments();
if (innerTArr == null || innerTArr.Length == 0)
Log("inner Type was not found. Cannot instantiate a generic class");

Type innerT = innerTArr[0];
Type genericType = t.GetGenericTypeDefinition();

Type[] typeArgs = { innerT };
Type constructed = genericType.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);
mac = o as IMac;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: