Ottenere Enum Lista via GetType
Domanda
Ho codice che fornisce un elenco di tutti i valori possibili per un dato enum
Ho legato abbastanza spesso DropDownList nella mia pagine web
ora im cercando di fare un controllo utente che accetta il nome del tipo come parametro, che a sua volta chiama il codice per creare l'elenco di valori
come il mio sub si aspetta un parametro di tipo
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
Nel tentativo di trasformare la stringa utilizzata nella usercontrols declration in un tipo. il problema è che posso farlo solo con tipi di sistema (anche non mscorlib, un po 'maldestramente). ma per le enumerazioni dichiarati nel mio App_Code, non riesco a capire il modo per farlo
l'AQN fa qualche stringa con un codice divertente come questo (AstroDate è il nome della mia classe):
"AstroDate, App_Code.rujpwg3d, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null"
ma se io uso nel gettype, esso errori
Si prega di avvisare
Modifica qui è il codice nel UserControl cercando di ottenere un elenco dei 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" è una proprietà stringa passata nel declartaion di UserControl nella pagina.
Soluzione 2
Ecco il codice
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 è solo un enum nel mio sistema, u possibile utilizzare qualsiasi u want
Grazie a tutti per cercare
Altri suggerimenti
ho avuto difficoltà di capire che cosa esattamente voleva fare. Così, mi sto prendendo una supposizione:
Avete un UserControl che crea un DrowDownList sulla base del tipo enum che fornisci. Ma, si aveva difficoltà a leggerlo di nuovo.
Ho creato un campione di lavoro che potrebbe essere utile a voi:
<%@ 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>
Codice dietro:
Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5
Fine Enum
Public Class WebForm1 Eredita 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
di controllo utente:
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>
Non sono sicuro che cosa sta succedendo, ma sto avendo problemi incollando il codice. Quindi, per favore nudo con esso e se qualcuno potesse risolvere il problema per me!