Create list of variable type

前端 未结 3 670
谎友^
谎友^ 2020-11-28 11:14

I am trying to create a list of a certain type.

I want to use the List notation but all I know is a \"System.Type\"

The type a have is variable. How can I cr

3条回答
  •  时光说笑
    2020-11-28 11:23

    Something like this should work.

    public IList createList(Type myType)
    {
        Type genericListType = typeof(List<>).MakeGenericType(myType);
        return (IList)Activator.CreateInstance(genericListType);
    }
    

提交回复
热议问题