比较封闭类型和开放类型

时间:2010-10-14 10:26:10

标签: c# reflection generics

我很好奇如何检查给定类型是否已关闭打开类型的版本。例如

public bool IsGenericList(Type source)
{
    return (source.IsGenericType &&
            /*here goes the manipulation on source type*/ == typeof(List<>));
}

1 个答案:

答案 0 :(得分:14)

尝试Type.GetGenericTypeDefinition

public bool IsGenericList(Type source)
{
    return source.IsGenericType &&
           source.GetGenericTypeDefinition() == typeof(List<>);
}