views:

24

answers:

1

I've done some research and found that there apparently is an easy and understandable way to do this using reflection.

Type MyType = typeof(MyObject);
IList lst = (IList)Activator.CreateInstance((typeof(List<>).MakeGenericType(MyType)));

I'm getting compile error. It's telling I do in fact need to supply the type to IList... Am I missing something?

Any help would be greatly appreciated, cheers.

+4  A: 

You forgot this:

using System.Collections;

without it, it only sees System.Collections.Generic.IList<T>, and therefore complains about that missing T.

Pavel Minaev