Making a generic type from a Type

List<Person> p = new List<Person>();

This works fine, but what about this?

Type someType = typeof(Person);
List<someType> p = new List<someType>();

Nope!

But you can do this…

Type genericType = typeof(List<>).MakeGenericType(someType);

Why would I want to? Because I am creating a tool that generates plain old .NET objects from an EcoSpace’s model, and I wanted to implement multi-role association ends as

public List<Building> RoleName;

rather than just

public Building[] RoleName;

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *