Recently I had this problem, where I had to get the Items and Values of an Enum within a class. The class name and enum name (as strings) are known and the instance itself has to be created at runtime. i.e; via Reflection
Namespace N1abcd
....
Public Class TestClass
Public Enum TestEnum
val1 = 20
val2 = 30
End Enum
End Class
The syntax in VB.net is as below
' System.Type.GetType("Namespace Name" + "." + "Class Name" + "+" + "Enum Name")
Dim fieldInfos() As System.Reflection.FieldInfo = System.Type.GetType("N1abcd.TestClass+TestEnum").GetFields
For Each f As System.Reflection.FieldInfo In fieldInfos
If f.IsLiteral Then
MsgBox(f.Name & " : " & CType(f.GetValue(Nothing), Integer) & vbCrLf)
End If
Next
Namespace N1abcd
....
Public Class TestClass
Public Enum TestEnum
val1 = 20
val2 = 30
End Enum
End Class
The syntax in VB.net is as below
' System.Type.GetType("Namespace Name" + "." + "Class Name" + "+" + "Enum Name")
Dim fieldInfos() As System.Reflection.FieldInfo = System.Type.GetType("N1abcd.TestClass+TestEnum").GetFields
For Each f As System.Reflection.FieldInfo In fieldInfos
If f.IsLiteral Then
MsgBox(f.Name & " : " & CType(f.GetValue(Nothing), Integer) & vbCrLf)
End If
Next
Comments
Post a Comment