Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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#比较本机类型和可空类型(Int32和Int32?)_C#_Generics_Types_Nullable - Fatal编程技术网

C#比较本机类型和可空类型(Int32和Int32?)

C#比较本机类型和可空类型(Int32和Int32?),c#,generics,types,nullable,C#,Generics,Types,Nullable,有没有一种方法可以比较C#中的可空泛型和不可空泛型 例如: public void function<T>() { Type t = sqlreader.GetValue(pos).GetType(); } public void函数() { 类型t=sqlreader.GetValue(pos.GetType(); } 其中,t为Int32类型,t为Nullable类型 我们如何比较t和t以使其返回true?调用Nullable.getUnderlineyType(t)

有没有一种方法可以比较C#中的可空泛型和不可空泛型

例如:

public void function<T>()
{
    Type t = sqlreader.GetValue(pos).GetType();
}
public void函数()
{
类型t=sqlreader.GetValue(pos.GetType();
}
其中,
t
Int32
类型,
t
Nullable
类型


我们如何比较
t
t
以使其返回
true

调用
Nullable.getUnderlineyType(t)

如果
t
可为空的
,则返回
typeof(X)
;否则,它将返回
null

因此,你可以写

t = Nullable.GetUnderlyingType(t) ?? t;
Type bigT = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

if (t == bigT) 

调用
Nullable.getUnderlineType(t)

如果
t
可为空的
,则返回
typeof(X)
;否则,它将返回
null

因此,你可以写

t = Nullable.GetUnderlyingType(t) ?? t;
Type bigT = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

if (t == bigT) 

不清楚您想做什么,但您可以使用:


不清楚您想做什么,但您可以使用: