【发布时间】:2018-05-27 13:12:52
【问题描述】:
我有一个类型变量,它是某种类型的列表。假设我不知道商品类型,如何获取商品类型?
类似
...
var type = property.PropertyType;
if (type == typeof(List<>) // I know this doesn't work
{ do something }
...
编辑:我什至试过type is Ilist 和type.IsGenericType,结果都是假的。
即使只是尝试
var a = (new List<int>()).GetType();
if ( a.IsGenericType && a is IList){} // both conditions return false
我不知道我做错了什么。
【问题讨论】:
-
我已经尝试了很多东西,但都不适合我。
-
var itemType = type.GetElementType();结果为空
-
Type itemType = type.GenericTypeArguments.First(); -
@Don'tmindme 首先,我建议编辑您的问题并使用您尝试
GetType()的实际代码更新它。您在问题中显示一件事,但评论显示别的东西。