使用反射获取类型的静态字段值

时间:2009-06-02 14:51:41

标签: .net vb.net reflection

我有一组静态“枚举”类,我用它来保存有意义的变量名来表示我在输入文件中收到的无意义的代码值。这是一个例子。

Public Class ReasonCodeValue
    Private Sub New()
    End Sub
    Public Shared ReadOnly ServiceNotCovered As String = "SNCV"
    Public Shared ReadOnly MemberNotEligible As String = "MNEL"
End Class

我想编写一个方法来接受这些静态类之一的类型和字符串值,并确定该值是否是静态字段值之一。我知道如何获取特定对象的实例字段,我知道如何获取特定类型的静态字段列表;我无法弄清楚的是如何在没有实例的情况下获取静态字段值。这是我到目前为止所做的。

Public Function IsValidValue(ByVal type As Type, ByVal value As String) As Boolean
    Dim fields = type.GetFields(BindingFlags.Public Or BindingFlags.Static)
    For Each field As FieldInfo In fields
        DoSomething()
    Next
End Function

我想我可以使枚举类非静态,这样我就可以在我的验证方法中创建一个传递给FieldInfo.GetValue的实例。如果可以的话,我宁愿保持我的课程。

我看到一个名为GetRawConstantValue的方法。它看起来很危险。这会给我我想要的东西吗?还有其他想法吗?

2 个答案:

答案 0 :(得分:24)

呼叫

field.GetValue(Nothing)

它会好的。您不需要静态成员的实例。

认为 GetRawConstantValue是你想要的 - 我会坚持上面的代码。

答案 1 :(得分:3)

从更广泛的意义上看你想要做的事情,也许这会更合适:

Public Interface ICustomEnum(Of T)
    Function FromT(ByVal value As T) As ICustomEnum(Of T)
    ReadOnly Property Value() As T

    ''// Implement using a private constructor that accepts and sets the Value property, 
    ''// one shared readonly property for each desired value in the enum,
    ''// and widening conversions to and from T.
    ''// Then see this link to get intellisense support
    ''// that exactly matches a normal enum:
    ''// https://stackoverflow.com/questions/102084/hidden-features-of-vb-net/102217#102217
End Interface

'
''' <completionlist cref="ReasonCodeValue"/>
Public NotInheritable Class ReasonCodeValue
    Implements ICustomEnum(Of String)

    Private _value As String
    Public ReadOnly Property Value() As String Implements ICustomEnum(Of String).Value
        Get
            Return _value
        End Get
    End Property

    Private Sub New(ByVal value As String)
        _value = value
    End Sub

    Private Shared _ServiceNotCovered As New ReasonCodeValue("SNCV")
    Public Shared ReadOnly Property ServiceNotCovered() As ReasonCodeValue
        Get
            Return _ServiceNotCovered
        End Get
    End Property

    Private Shared _MemberNotEligible As New ReasonCodeValue("MNEL")
    Public Shared ReadOnly Property MemberNotEligible() As ReasonCodeValue
        Get
            Return _MemberNotEligible
        End Get
    End Property

    Public Shared Function FromString(ByVal value As String) As ICustomEnum(Of String)
        ''// use reflection or a dictionary here if you have a lot of values
        Select Case value
            Case "SNCV"
                Return _ServiceNotCovered
            Case "MNEL"
                Return _MemberNotEligible
            Case Else
                Return Nothing ''//or throw an exception
        End Select
    End Function

    Public Function FromT(ByVal value As String) As ICustomEnum(Of String) Implements ICustomEnum(Of String).FromT
        Return FromString(value)
    End Function

    Public Shared Widening Operator CType(ByVal item As ReasonCodeValue) As String
        Return item.Value
    End Operator

    Public Shared Widening Operator CType(ByVal value As String) As ReasonCodeValue
        Return FromString(value)
    End Operator
End Class

然后使用此链接,您可以获得与正常枚举完全匹配的智能感知支持:
Hidden Features of VB.NET?