Create list of variable type

前端 未结 3 669
谎友^
谎友^ 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:32

    You could use Reflections, here is a sample:

        Type mytype = typeof (int);
    
        Type listGenericType = typeof (List<>);
    
        Type list = listGenericType.MakeGenericType(mytype);
    
        ConstructorInfo ci = list.GetConstructor(new Type[] {});
    
        List listInt = (List)ci.Invoke(new object[] {});
    

提交回复
热议问题