Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查类型是否为可为空的系统类型_C#_.net - Fatal编程技术网

C# 如何检查类型是否为可为空的系统类型

C# 如何检查类型是否为可为空的系统类型,c#,.net,C#,.net,我正在尝试编写一个方法,该方法接受一个类型并确定它是用户定义的类还是系统类。我的代码如下。 方法: 系统类型 public static List<Type> SystemTypes { get { if (_systemTypes == null) { _systemTypes = Assembly.GetExecutingAssembly().GetType().Module.Assembly.GetExpo

我正在尝试编写一个方法,该方法接受一个类型并确定它是用户定义的类还是系统类。我的代码如下。 方法:

系统类型

public static List<Type> SystemTypes
{
    get
    {
        if (_systemTypes == null)
        {
            _systemTypes = Assembly.GetExecutingAssembly().GetType().Module.Assembly.GetExportedTypes().ToList();
        }
        return _systemTypes;
    }
}
公共静态列表系统类型
{
得到
{
如果(_systemTypes==null)
{
_systemTypes=Assembly.GetExecutionGassembly().GetType().Module.Assembly.GetExportedTypes().ToList();
}
返回系统类型;
}
}

这适用于不可为空的类型。但是,当我得到一个类型,如
int?
,它以
{System.Nullable`1[System.Int32]}
的形式出现,它返回false,因为它在列表中找不到该类型。关于如何检查空值,或者运行此比较的更好方法,您有什么想法吗?

也许您可以检查名称空间是否以
System
开头,并在
nullable
的情况下获取基础类型

public static bool IsSystemType(Type type)
{
    var nullableType = Nullable.GetUnderlyingType(type);
    if(nullableType != null)
        type = nullableType;
    return type.Namespace.StartsWith("System");

}
但是,当我得到一个类型,如
int?
,它以
{System.Nullable`1[System.Int32]}
的形式出现,它返回false,因为它在列表中找不到该类型

您可以测试所有具体的泛型类型,确定其类型定义是否为可为null的`1,然后查找其泛型类型参数:

private static bool isNotCustomClass(Type type)
{
    if(type.IsGenericType && !type.IsGenericTypeDefinition && type.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
        // Nullable<T> detected, test T instead
        type = type.GenericTypeArguments[0]
    }

    return SystemTypes.Contains(type);
}
private static bool isNotCustomClass(类型)
{
if(type.IsGenericType&&!type.IsGenericTypeDefinition&&type.GetGenericTypeDefinition()==typeof(可为空))
{
//检测到可为空,请改为测试T
type=type.GenericTypeArguments[0]
}
返回系统类型。包含(类型);
}

我被你把“系统类”和“可空”混为一谈搞糊涂了。什么是“系统类”?@JonathanAlfaro,我认为,这应该意味着OP可以在他的代码中检查该类型,不是吗?提示:您可能会发现
可以为null。GetUnderlyingType
很有用。@JonathanAlfaro请告诉其他人怀疑的好处,准备好向任何人学习,不要总是相信自己什么都知道。其中是.NET和CLR的规范,第II.9节,它说:“
List``1
是泛型类型;它有时被称为泛型类型或开放泛型类型,因为它至少有一个泛型参数。此分区将使用术语“打开类型”<代码>列表“%1是封闭的泛型类型,因为它没有未绑定的泛型参数@乔纳森·法罗。。。。(有时称为实例化泛型类型或泛型类型实例化)。此分区将使用术语closed type“您还可以在第9.4节中找到定义C#spec的,关于构造类型的内容。我承认最后一个概念稍微有点细微:构造意味着它被赋予了类型参数,但是如果参数本身是开放的,我们仍然认为它是一个开放类型。是,
Nullable
使用。。。。
private static bool isNotCustomClass(Type type)
{
    if(type.IsGenericType && !type.IsGenericTypeDefinition && type.GetGenericTypeDefinition() == typeof(Nullable<>))
    {
        // Nullable<T> detected, test T instead
        type = type.GenericTypeArguments[0]
    }

    return SystemTypes.Contains(type);
}