bool IsTypeAGenericList(Type listType)
{
typeof(IList<>).IsAssignableFrom(listType.GetGenericTypeDefinition())
}
给定 typeof(List<int>)
时返回 false .
我假设这是因为两个类型参数可以不同,对吗?
最佳答案
实际上,这是可行的:
public static bool IsGenericList(Type type)
{
if (!type.IsGenericType)
return false;
var genericArguments = type.GetGenericArguments();
if (genericArguments.Length != 1)
return false;
var listType = typeof (IList<>).MakeGenericType(genericArguments);
return listType.IsAssignableFrom(type);
}
关于c# - 不是可以从通用列表分配通用 IList 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1177434/