Get Enum List via GetType
Pergunta
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.
Solução 2
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
Outras dicas
I had difficulties understanding what you exactly wanted to do. So, I am taking a guess :
You have one UserControl which creates a DrowDownList based on the Enum Type you provide. But, you had difficulties reading it back.
I created a working sample which might be helpful to you :
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1" %>
<%@ Register src="DynamicComboFromEnum.ascx" tagname="DynamicComboFromEnum" tagprefix="uc1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:DynamicComboFromEnum ID="DynamicComboFromEnum1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Code behind :
Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5
End Enum
Public Class WebForm1 Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
DynamicComboFromEnum1.EnumType = GetType(TestEnum)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Label1.Text = DynamicComboFromEnum1.GetSelectedValue().ToString()
End Sub
End Class
User Control :
Public Class DynamicComboFromEnum
Inherits System.Web.UI.UserControl
Public Property EnumType() As Type
Get
Return ViewState("EnumType")
End Get
Set(ByVal value As Type)
ViewState("EnumType") = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack) Then
RefillData()
End If
End Sub
Sub RefillData()
MainList.Items.Clear()
MainList.DataSource = EnumList(EnumType)
MainList.DataBind()
End Sub
Private Function EnumList(ByVal type As Type) As Object
Dim Names As String() = [Enum].GetNames(type)
Return Names
End Function
Public Function GetSelectedValue() As Object
Return [Enum].Parse(EnumType, MainList.SelectedValue)
End Function
End Class
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DynamicComboFromEnum.ascx.vb" Inherits="WebApplication2.DynamicComboFromEnum" %>
<asp:DropDownList ID="MainList" runat="server"></asp:DropDownList>
I am not sure what is going on but I am having problems pasting the code. So, please bare with it and if someone could fix it up for me!