C# How to create generic List with dynamic type


 Given the Type typeof(Item) (defined below), how would you create a list of Items using Reflection in C#?

var type = typeof(Item);
IList listOfItems = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
    
public class Item
{
    public string Name { getset; }
}
    
    

    

Comments

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?