BC32126:"System.Nullable(Of T)" 的方法不能用作 "AddressOf" 运算符的操作数

语句将 AddressOf 运算符与表示 Nullable<T> 结构过程的操作数一起使用。

错误 ID: BC32126

更正此错误

  • AddressOf 子句中的过程名称替换为不是 Nullable<T> 的成员的操作数。

  • 编写一个类,用于包装要使用的 Nullable<T> 的方法。 在以下示例中,NullableWrapper 类定义了命名为 GetValueOrDefault 的新方法。 由于此新方法不是 Nullable<T> 的成员,因此可将其应用到 nullInstance,可以为 null 的类型的实例,以形成 AddressOf 的参数。

Module Module1

    Delegate Function Deleg() As Integer

    Sub Main()
        Dim nullInstance As New Nullable(Of Integer)(1)

        Dim del As Deleg

        ' GetValueOrDefault is a method of the Nullable generic
        ' type. It cannot be used as an operand of AddressOf.
        ' del = AddressOf nullInstance.GetValueOrDefault

        ' The following line uses the GetValueOrDefault method
        ' defined in the NullableWrapper class.
        del = AddressOf (New NullableWrapper(
            Of Integer)(nullInstance)).GetValueOrDefault

        Console.WriteLine(del.Invoke())
    End Sub

    Class NullableWrapper(Of T As Structure)
        Private m_Value As Nullable(Of T)

        Sub New(ByVal Value As Nullable(Of T))
            m_Value = Value
        End Sub

        Public Function GetValueOrDefault() As T
            Return m_Value.Value
        End Function
    End Class
End Module

另请参阅