c# - 使用反射的通用列表

标签 c# reflection generic-list

我有一个场景,我得到了 Class name as a string 我只想创建该类的通用 List。我尝试了以下

            Type _type = Type.GetType(className);

            List<_type> list = new List<_type>();

但是我收到错误。我不能把那个 _type 作为列表参数。如果有人知道请帮助我

最佳答案

最接近的:

Type listType = typeof(List<>).MakeGenericType(_type);
IList list = (IList) Activator.CreateInstance(listType);

关于c# - 使用反射的通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509695/

相关文章:

c# - Caliburn 微 : Disable button on form validation error

java - 如何实例化 Android Activity (使用反射)?

c# - 排序列表<xyz <String,String >>,错误: InvalidOperationException的问题

c# - C# 中的泛型列表类

c# - WPF ListBox 自定义控件问题

c# - 从 C# WinForm 将数据发布到 PHP 页面

c# - 有没有在 XAML 中整合类似的数据绑定(bind)和/或触发器?

java - 我们如何在 java 中使用反射将 transient 添加到现有类?

c# - 获取类型的默认构造函数的最有效方法

delphi - 如何在 Delphi 调试期间查看通用 tList