How to create a shared list with a dynamic object type

I want to create a generic list of a Type object.

I have...

Type type = typeof(Foo); object model = GetModel(); 

Now I want to create a new List<Foo>((Foo)model)

Is this possible in C #?

+3
list generics c # dynamic
source share
1 answer
 Type listT = typeof(List<>).MakeGenericType(new[]{type}); object list = Activator.CreateInstance(listT); 
+10
source share

All Articles