c# - 获取泛型 List<T> 对象的 T 类型

标签 c# list generics reflection

<分区>

Possible Duplicate:
Reflection - Getting the generic parameters from a System.Type instance
Get actual type of T in a generic List<T>

我正在循环访问类中的 PropertyInfo-List。 在这门课中,我对不同的列表感兴趣——例如 List<int> , List<string> , List<Book> , aso.

但我不知道如何获得列表对象的类型。我只是得到类似的东西

System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089]].

我可以将这些信息分开并从这些信息中创建一个类型,但我希望有更顺畅的方法吗?

最佳答案

简单:

Type type = list.GetType().GetGenericArguments()[0];
// The first argument will be the `T` inside `List<T>`... so the list type ;)

关于c# - 获取泛型 List<T> 对象的 T 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10341538/

相关文章:

python - 列表列表更改意外地反射(reflect)在子列表中

java - 如何修复索引越界错误?

java - 将@interface传递给 'data'类,然后显示字段 'getName()'

c# - .net 5、secrets.json、appsettings.json 和 Azure 应用程序设置

c# - WPF:从模型MVVM关闭窗口

Python - 按字母顺序排列嵌套列表

Java arraylist 找不到构造函数,使用 arrays.aslist

c# - 泛型方法的调用是不明确的错误

c# - 如何在 C# 中使用 MapReduce 连接 2 个集合?

c# - 如何通过 Dapper 传播 SQL 消息选项卡输出