2

enum是否可以使用string索引返回值?例如我可以使用:

Enum test
    firstval
    secondval
    thirdval
End Enum

Dim index As Integer = 1

CType(index, test).ToString()

返回firstval,但有没有办法在索引是一个值的情况下做类似的事情string?例如:

Enum test
    firstval = "one"
    secondval = "two"
    thirdval = "three"
End Enum

Dim index As string = "one"

CType(index, test).ToString()
4

2 回答 2

3

使用 是不可能的Enum,但您可以使用运算符轻松创建可以执行您想要的Narrowing操作的类型。

简单的例子:

Class Test

    Private Shared _lookup As Dictionary(Of String, Test)

    Private Key As String
    Private Name As String

    Public Shared ReadOnly firstval  As Test = New Test("one", "firstval")
    Public Shared ReadOnly secondval As Test = New Test("two", "secondval")
    Public Shared ReadOnly thirdval  As Test = New Test("three", "thirdval")

    Private Sub New(key As String, name As String)
        Me.Key = key
        Me.Name = name
        If _lookup Is Nothing Then _
            _lookup = New Dictionary(Of String, Test)

        _lookup.Add(key, Me)
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Name ' or whatever you want '
    End Function

    Public Shared Widening Operator CType(obj As Test) As String 
        Return obj.Key
    End Operator 

    Public Shared Narrowing Operator CType(key As String) As Test
        Return _lookup(key)
    End Operator 

End Class

用法:

Dim index As string = "one"

' returns firstval '
CType(index, Test).ToString() 
于 2013-09-18T09:08:39.490 回答
1

还有其他几种选择。

一种是获取枚举中使用的名称。例如:

Friend Enum ImgFormat
    Bitmap
    GIF
    JPeg
    TIFF
    PNG
End Enum

Dim ImgNames() As String
...
ImgNames = [Enum].GetNames(GetType(ImgFormat))

如果你的名字不够友好,用描述来装饰它们:

Imports System.ComponentModel

Friend Enum ImgFormat
    <Description("Bitmap (BMP)")> Bitmap
    <Description("Graphic Interchange (GIF)")> GIF
    <Description("Jpg/JPeg (JPG)")> JPeg
    <Description("Tagged Image (TIFF)")> TIFF
    <Description("Portable Graphics (PNG)")> PNG
End Enum

要获得描述,需要涉及的反射:

Imports System.Reflection
Imports System.ComponentModel

Public Class EnumConverter
' gets a single enum description
Public Shared Function GetEnumDescription(ByVal EnumConstant As [Enum]) As String
    Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
    Dim attr() As DescriptionAttribute = _
        DirectCast( _
            fi.GetCustomAttributes(GetType(DescriptionAttribute), False), _
            DescriptionAttribute() )

       If attr.Length > 0 Then
           Return attr(0).Description
       Else
           Return EnumConstant.ToString()
       End If
End Function

' get all the enum descriptions:
Public Shared Function GetEnumDescriptions(ByVal type As Type) As String()
    Dim n As Integer = 0

    Dim enumValues As Array = [Enum].GetValues(type)
    Dim Descr(enumValues.Length - 1) As String

    For Each value As [Enum] In enumValues
         Descr(n) = GetEnumDescription(value)
         n += 1
    Next

    Return Descr

End Function
End Class

要使用:

Dim ImgNames() As String = EnumConverter.GetEnumDescriptions(ImgFormat)

ImgNames(ImgFormat.GIF)将是“图形交换 (GIF)”

如果枚举值不是默认的 0、1、2 ... 如果这是一个问题(确实是),这将中断,然后围绕它构建一个类来存储带有枚举值的名称或描述。与其构建一个类来创建伪枚举,不如创建一个由描述和枚举值组成的名称-值对列表。

于 2013-09-18T14:44:41.900 回答