Search code examples
typesenumsapp-codegettype

Get Enum List via GetType


I have code that gives a list of all possible values for any given enum
i bound it pretty often to dropdownlists in my webpages
now im trying to make a usercontrol which accepts the type name as a parameter, which in turn calls the code to create the value list
as my sub expects a type parameter

Shared Function EnumList(ByVal EnumType As Type) As List(Of ListItem)
        Dim ret As New List(Of ListItem)
        Dim consts = [Enum].GetValues(EnumType)
            For Each c In consts
                ret.Add(New ListItem With {.Text = c.ToString, .Value = c.ToString})
            Next
        Return ret
    End Function

im trying to turn the string used in the usercontrols declration into a type. the problem is i can only do it with system types (even non mscorlib, a bit clumsily). but for enums declared in my app_code, i cannot figure out the way to do it
the aqn makes some string with a funny code like this (AstroDate is the name of my class):
"AstroDate, App_Code.rujpwg3d, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
but if i use it in the gettype, it errors

please advise

Edit here is the code in the usercontrol trying to get a list of the Enum

    Sub RefillData()
    Dim TempValue = Value
    MainList.Items.Clear()
    MainList.DataSource = EnumList(Type.GetType(EnumType, True, True))
    If EmptyText <> "" Then
        Dim itm As New ListItem(EmptyText, "")
        MainList.Items.Add(itm)
    End If
    MainList.DataBind()
    Value = TempValue
End Sub

"EnumType" is a string property passed in the declartaion of the usercontrol in the page.


Solution

  • heres the code

    Dim ax = Reflection.Assembly.Load(ObjectType.Account.GetType.Assembly.FullName)
    Dim tx = ax.GetType(EnumType)
    Dim enumers = [Enum].GetNames(tx)
    System.Array.Sort(enumers)
    e.Result = enumers
    

    objecttype.account is just any enum in my system, u can use whichever u want

    thanks all for trying