c# - 封闭式与开放式比较

标签 c# reflection generics

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

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

最佳答案

尝试 Type.GetGenericTypeDefinition :

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

关于c# - 封闭式与开放式比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932193/

相关文章:

c# - Linq 按不排序排序

c# - SQL 批量存储过程调用 C#

c# - 如何在 C# 中将函数作为参数传递?

java - 如何制作.class的 map ?

list - Web API 使用附加属性扩展列表

ios - Swift - 将参数从非泛型函数传递到泛型函数

java - 将类类型传递给方法,然后转换为该类型?

c# - 如何减少和清除内存泄漏?

c# - 使用 OneDrive API 创建 App 文件夹并上传文件

C# 相当于字段的 typeof