Type type = pi.PropertyType;
if(type.IsGenericType && type.GetGenericTypeDeFinition()
== typeof(List<>))
{
Type itemType = type.GetGenericArguments()[0]; // use this...
}
通常,要支持any IList<T>
,您需要检查接口:
foreach (Type interfaceType in type.GetInterfaces())
{
if (interfaceType.IsGenericType &&
interfaceType.GetGenericTypeDeFinition()
== typeof(IList<>))
{
Type itemType = type.GetGenericArguments()[0];
// do something...
break;
}
}