且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

确定泛型参数是否为 Nullable 类型

更新时间:2023-12-01 08:03:22

一种方法是:

If Nullable.GetUnderlyingType(GetType(TData)) <> Nothing

...至少,C# 是:

... at least, the C# is:

if (Nullable.GetUnderlyingType(typeof(TData)) != null)

假设您在询问它是否为可空值类型.如果您问它是可空值类型还是类,那么 C# 版本将是:

That's assuming you're asking whether it's a nullable value type. If you're asking whether it's a nullable value type or a class then the C# version would be:

if (default(TData) == null)

但我不确定简单的 VB 翻译是否适用于那里,因为Nothing"在 VB 中略有不同.

but I'm not sure whether a simple VB translation would work there, as "Nothing" is slightly different in VB.