如何在Visual Basic .NET中支持自定义对象的上下文隐式转换?

时间:2019-01-03 14:54:58

标签: vb.net object casting

我想在我的应用程序中使用命名错误代码。这样可以确保每个开发人员都不会将纯数字错误代码与其他代码混淆,并且还可以减少开发人员认识错误代码应代表的时间。

比较此示例:

Function New() As Integer
    Return 0
End Function

在此示例中:

Function New() As Integer
    Return ErrorCodes.ERROR_SUCCESS
End Function

当然,我可以让开发人员编写如下内容:

Function New() As Integer
    Return 0 ' ERROR_SUCCESS
End Function

但是,当开发人员更新实际的返回代码但忘记了注释时,上面的代码会带来一个陷阱。一些开发人员查看实际的返回代码,另一些查看注释。我想减轻这种混乱。

我上了下面的课(摘录):

Public Class ErrorCodes
    Private msName As String = Nothing
    Private miValue As Integer = 0

    Public Shared ReadOnly ERROR_SUCCESS As ErrorCodes = New ErrorCodes("ERROR_SUCCESS", 0)

    Private Sub New(ByVal psName As String, ByVal piValue As Integer)
        msName = psName
        miValue = piValue
    End Sub

    Public ReadOnly Property [Name] As String
        Get
            Return msName
        End Get
    End Property

    Public ReadOnly Property [Value] As Integer
        Get
            Return miValue
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return String.Format("[{0}]{1}", msName, miValue)
    End Function
End Class

现在,我想像下面的示例一样使用此类ErrorCodes

Function New() As Integer
    Return ErrorCodes.ERROR_SUCCESS
End Function

按预期的那样,由于返回的实际值是类ErrorCodes的实例,而不是通用数据类型Integer的实例,因此我将产生异常(类型转换)。

您可以使用ToString()函数看到,当将类实例分配给{{1}时,我让类自动/隐式将实例化对象转换为通用数据类型String。 }类型的变量。

是否可以像对String一样对通用数据类型Integer进行相同操作?

出于与Windows XP SP3兼容性的原因,我正在使用.NET Framework 4.0。

另一种表达我想要的方式:

ToString()

我不想触发隐式转换警告/错误,也不想强迫开发人员明确进行类型转换。

2 个答案:

答案 0 :(得分:2)

正如jmcilhinney指出的那样,它使用Enums和Description属性。

这是班上

'requires
'  Imports System.Reflection
'  Imports System.ComponentModel
Public Class ErrorCodes
    Public Enum ErrCode 'these are the error codes
        'note that the codes should be unique
        <Description("Success")> ERROR_SUCCESS = 0
        <Description("Error A")> ERROR_A = 1
    End Enum

    Public Class InfoForErrCode
        Public TheDescription As String
        Public TheValue As Integer
        Public AsString As String
    End Class

    Public Shared Function Info(TheError As ErrCode) As InfoForErrCode
        Dim rv As New InfoForErrCode
        rv.TheDescription = GetDescription(TheError)
        rv.TheValue = TheError
        rv.AsString = TheError.ToString
        Return rv
    End Function

    Private Shared Function GetDescription(TheError As ErrCode) As String
        Dim rv As String = ""
        Dim fi As FieldInfo = TheError.GetType().GetField(TheError.ToString())

        Dim attr() As DescriptionAttribute
        attr = DirectCast(fi.GetCustomAttributes(GetType(DescriptionAttribute),
                                                 False), DescriptionAttribute())

        If attr.Length > 0 Then
            rv = attr(0).Description
        Else
            rv = TheError.ToString()
        End If
        Return rv
    End Function
End Class

这是如何使用它

    Dim foo As ErrorCodes.ErrCode = ErrorCodes.ErrCode.ERROR_SUCCESS
    Dim inf As ErrorCodes.InfoForErrCode = ErrorCodes.Info(foo)
    Stop 'examine inf
    foo = ErrorCodes.ErrCode.ERROR_A
    inf = ErrorCodes.Info(foo)
    Stop 'examine inf

答案 1 :(得分:2)

是的,您可以使用转换运算符来实现。 这是代码:

Public Class Form1
  Public Class ErrorCodes
    Private msName As String = Nothing
    Private miValue As Integer = 0

    Public Shared Widening Operator CType(ByVal ec As ErrorCodes) As String
        Return ec.ToString
    End Operator

    Public Shared Narrowing Operator CType(ByVal ec As ErrorCodes) As Integer
        Return ec.Value
    End Operator

    Public Shared ReadOnly ERROR_SUCCESS As ErrorCodes = New ErrorCodes("ERROR_SUCCESS", 0)
    Public Shared ReadOnly ERROR_FAILED As ErrorCodes = New ErrorCodes("ERROR_FAILED", 1)

    Private Sub New(ByVal psName As String, ByVal piValue As Integer)
        msName = psName
        miValue = piValue
    End Sub

    Public ReadOnly Property [Name] As String
        Get
            Return msName
        End Get
    End Property

    Public ReadOnly Property [Value] As Integer
        Get
            Return miValue
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return String.Format("[{0}]{1}", msName, miValue)
    End Function
  End Class

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim em As String = ErrorCodes.ERROR_SUCCESS
    Dim ev As Integer = ErrorCodes.ERROR_SUCCESS
    Dim mm As String = String.Format("String: {0}, Value: {1}", em, ev)

    MsgBox(mm)
  End Sub
End Class

更多信息here

希望这会有所帮助。