如何从列表类型中获取模板类型?
我已经四处寻找这个有一段时间了。我想知道如何获取传递到创建列表的模板类型。例如。
List<string> listOfStrings = new List<string>();
我如何从中获取字符串类型?我知道如何使用类型来获取字段并查找列表类型,但不知道其中的模板。
I've been looking around for this for awhile. I would like to know how to get the type of the template passed into creating a list. For example.
List<string> listOfStrings = new List<string>();
How would I get the string type from this? I know how to use the type to get the fields and find the list type but not the template within it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这种情况下,你可以这样得到它,
注意,如果你在非泛型类型上尝试这个,它会失败,因为GetGenericArguments数组的长度将为0,但是你当然可以采取措施来控制数组的长度。
In this instance, you can get it like this
Note that if you try this on a non generic type, it will fail because the length of the GetGenericArguments array will be 0, but you can of course take measures to control the length of the array.
您是在谈论获取存储在此列表中的项目类型吗?
将返回您的类型。
编辑:
这是正确的解决方案:
Are you talking about getting type of items that are stored in this list?
will return you the type.
Edit:
This is the correct solution:
但也许他想知道如何在调用函数时获取 T...
But perhaps he wants to know how to get the T when calling a function...