.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>