how to create a list of type obtained from reflection

后端 未结 3 1637
一个人的身影
一个人的身影 2021-01-21 14:04

I have a code which looks like this :

Assembly assembly = Assembly.LoadFrom(\"ReflectionTest.dll\");
Type myType = assembly.GetType(@\"ReflectionTest.TestObject\         


        
3条回答
  •  [愿得一人]
    2021-01-21 14:43

    Try this:

    var listType = typeof(List<>);
    var constructedListType = listType.MakeGenericType(myType);
    
    var myList = (IList)Activator.CreateInstance(constructedListType);
    myList.Add(x);
    

    The list will not be strongly-typed but you can add items as objects.

提交回复
热议问题