确定泛型参数是否为 Nullable 类型 [英] Determine if a generic param is a Nullable type

查看:24
本文介绍了确定泛型参数是否为 Nullable 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下VB.NET函数,例如:

I have the following VB.NET function, for example:

Public Function MyFunction (Of TData) (ByVal InParam As Integer) As TData

End Sub

如何在函数中确定 TData 是否为 NULLable 类型?

How do I, in a function, determine if TData is a NULLable Type?

推荐答案

一种方法是:

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.

这篇关于确定泛型参数是否为 Nullable 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆