C#比较本机类型和可空类型(Int32和Int32?)
有没有一种方法可以比较C#中的可空泛型和不可空泛型 例如: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)
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)
不清楚您想做什么,但您可以使用:
不清楚您想做什么,但您可以使用: