Dot-Net

使用反射獲取類型的靜態欄位值

  • September 21, 2009

我有一組靜態“列舉”類,我用它們來保存有意義的變數名,以表示我在輸入文件中收到的無意義的程式碼值。這是一個例子。

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. 看起來很危險。這會給我我想要的東西嗎?還有其他想法嗎?

稱呼

field.GetValue(Nothing)

會好的。您不需要靜態成員的實例。

我不認為 GetRawConstantValue這是你想要的 - 我會堅持上面的程式碼。

從更大的意義上看你想要做的事情,也許這會更合適:

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

然後使用此連結,您可能可以獲得與普通列舉完全匹配的智能感知支持:

VB.NET 的隱藏功能?

引用自:https://stackoverflow.com/questions/940002