Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何确定泛型容器存储的类型?_.net_Reflection - Fatal编程技术网

.net 如何确定泛型容器存储的类型?

.net 如何确定泛型容器存储的类型?,.net,reflection,.net,Reflection,我正在查询一个对象属性,查找属于特定类型TypeOfInterest的属性,或者是存储TypeOfInterest对象的泛型属性。对于一个列表(共T个)我如何判断T是TypeOfInterest Console.WriteLine(typeof(List<string>).GetGenericArguments()[0] == typeof(string)); Console.WriteLine(typeof(List).GetGenericArguments()[0]==type

我正在查询一个对象属性,查找属于特定类型
TypeOfInterest
的属性,或者是存储
TypeOfInterest
对象的泛型属性。对于一个
列表(共T个)
我如何判断
T
TypeOfInterest

Console.WriteLine(typeof(List<string>).GetGenericArguments()[0] == typeof(string));
Console.WriteLine(typeof(List).GetGenericArguments()[0]==typeof(string));
您的列表就是列表
如果要确保列表仅包含该类的变量,则需要检查每个变量。

此页面显示了如何:

简而言之,您可以从列表中获取类型值,然后使用GetGenericArguments方法

YourList is List<TypeOfInterest>