让我们有这样的常量:
Function GetString(index1 as String, index2 as String) as String
Dim index as String = index1 & index2
Return ' ... here I need the magic to retrieve the specific constant resulted from the combination of those two indexes ...
End Function
比,在Sub中的elswhere检索这样的常量值:
{{1}}
使用Select Case语句不是一个选项,因为有大量的常量和它们的可能组合。此外,任何其他方法都不仅仅是好的。
答案 0 :(得分:0)
根据此处的答案并进行了扩展:https://stackoverflow.com/a/10261848/1856451
Private Function GetConstantValue(type As Type, name As String) As String
Dim fieldInfos As FieldInfo() = type.GetFields(BindingFlags.[Public] Or BindingFlags.[Static] Or BindingFlags.FlattenHierarchy)
Dim field = fieldInfos.FirstOrDefault(Function(fi) fi.IsLiteral AndAlso Not fi.IsInitOnly AndAlso fi.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
If field IsNot Nothing Then
Return field.GetValue(Me).ToString()
Else
Return "" 'Or throw an exception or something
End If
End Function