Rookian
Rookian

Reputation: 20569

How to create a generic List with a dynamic object type

I want to create a generic list of the 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#?

Upvotes: 3

Views: 4722

Answers (1)

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

Type listT = typeof(List<>).MakeGenericType(new[]{type});
object list = Activator.CreateInstance(listT);

Upvotes: 10

Related Questions