Reputation: 340
I want to refer to an enum by the string name of its root. Please note, i wish to refer to the enum, not an enum member.
There are many posts on stackoverflow describing how to refer an enum member by its name (eg How to retrieve an Enum member given its name), but i didn't find any about how to refer to the enum by the name of it's root.
To further clarify;
Enum MyEnumA : Quiet : Noisy : End enum
Enum MyEnumB : Big : Small : Gigantic : End enum
Sub Foo(strAction as string)
' Depending on value of strAction, i want to create a list of either MyEnumA or MyEnumB members
' I know i can't do the following, it's just to make clear the direction i'm wanting to go -
Dim lstMembers As New List(Of CType(strAction,[Enum]))
'....
end function
Following the good suggestions below, i've tried this;
Dim enumType As Type = System.Type.GetType("ExcelInterface.clsBTAnalyseRslts+" & "strAction")
Dim lstFldIndx As New List(Of enumtype) 'Fails to compile this line as doesn't recognize enumtype as defined
Thank you!
Upvotes: 1
Views: 2313
Reputation: 8256
Ric's answer didn't quite work for me because my Enum wasn't in the same location as my code, so here's how I finally managed it (I use VB, but I'll add C# for reference):
Dim EnumName As String = "MyEnum"
Dim MyAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetAssembly(GetType(My.Assembly.Location))
Dim MyEnum As System.Type = MyAssembly.GetType("My.Assembly.Location+ClassName+" & EnumName)
Dim EnumNames As String() = MyEnum.GetEnumNames()
For intVal As Integer = 0 To EnumNames.Length - 1
' EnumNames(intVal) = Name of Enum Value
' intVal = Enum Value
Next
string EnumName = "MyEnum";
System.Reflection.Assembly MyAssembly = System.Reflection.Assembly.GetAssembly(typeof(My.Assembly.Location));
System.Type MyEnum = MyAssembly.GetType("My.Assembly.Location+ClassName+" + EnumName);
string[] EnumNames = MyEnum.GetEnumNames();
for (int intVal = 0; intVal <= EnumNames.Length - 1; intVal++) {
// EnumNames(intVal) = Name of Enum Value
// intVal = Enum Value
}
Hopefully it will save someone's valuable time :).
Upvotes: 0
Reputation: 2660
Here's a code block that converts the enum into a string and then gets the type based on the string. It will show you how to get the type and how you should format the string. The rest should be pretty straight forward.
Dim obj As MyEnumA
Dim t As Type = obj.GetType()
Dim s As String = t.FullName
Dim t2 As Type = System.Type.GetType(s)
Then do this to get the values:
Dim Members() As String
Members = System.Enum.GetNames(t2)
Variable s will look something like this "namespace.class + MyEnumA" so all you need to do is to create this string programmatically and send it to a function.
Upvotes: 1
Reputation: 13248
Give this a go, it creates an array so you can change it to a List(Of x...) later:
Sub Foo(ByVal strAction As String)
Dim exAssembly = Reflection.Assembly.GetExecutingAssembly
Dim enumType = exAssembly.GetTypes.First(Function(f) f.Name = strAction)
Dim myEnum = [Enum].GetValues(enumType)
End Sub
I found an interesting post here which gave me some direction.
usage:
Foo("MyEnumA")
I will leave you to do some error handling and checking :D
Upvotes: 1