2009-06-02 74 views
8

我有一组静态“枚举”类,用于保存有意义的变量名以表示在输入文件上收到的无意义代码值。这是一个例子。使用反射获取类型的静态字段值

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的方法。它看起来很危险。这会给我我想要的东西吗?任何其他想法?

回答

24

呼叫

field.GetValue(Nothing) 

,它会被罚款。您不需要静态成员的实例。

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

+1

嘿,你把它放在VB中为我。从未想过要传递null。谢谢。 – 2009-06-02 14:55:36

+0

在VB中提出一个问题,我会尝试在VB中回答它。有时它会出错,但在这种情况下,它不是太难:) – 2009-06-02 14:59:37

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?