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

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

End Sub

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

最佳答案

一种方法是:

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

...至少,C#是:
if (Nullable.GetUnderlyingType(typeof(TData)) != null)

假设您要询问它是否为可空值类型。如果您询问它是可为空的值类型还是类,则C#版本将为:
if (default(TData) == null)

但是我不确定是否可以在其中进行简单的VB转换,因为VB中的“Nothing”略有不同。

关于.net - 确定泛型参数是否为Nullable类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5181494/

10-12 20:02