Depending on how often you're calling this method then using Activator.CreateInstance could be slow. Another option is to do something like this:
private Dictionary> delegates = new Dictionary>();
public Dictionary<Type, object> GenerateLists(List<Type> types)
{
Dictionary<Type, object> lists = new Dictionary<Type, object>();
foreach (Type type in types)
{
if (!delegates.ContainsKey(type))
delegates.Add(type, CreateListDelegate(type));
lists.Add(type, delegates[type]());
}
return lists;
}
private Func<object> CreateListDelegate(Type type)
{
MethodInfo createListMethod = GetType().GetMethod("CreateList");
MethodInfo genericCreateListMethod = createListMethod.MakeGenericMethod(type);
return Delegate.CreateDelegate(typeof(Func<object>), this, genericCreateListMethod) as Func<object>;
}
public object CreateList<T>()
{
return new List<T>();
}
On the first hit it'll create a delegate to the generic method that creates the list and then puts that in a dictionary. On each subsequent hit you'll just call the delegate for that type.
Hope this helps!