Pregunta

Tengo código que proporciona una lista de todos los valores posibles para cualquier enumeración dada
i limitó bastante a menudo para dropdownlists en mi páginas
Ahora estoy tratando de hacer un control de usuario que acepta el nombre del tipo como un parámetro, que a su vez llama al código para crear la lista de valores
como mi sub espera un parámetro de 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

Im tratando de convertir la cadena que se utiliza en el declration controles de usuario en un tipo. el problema es que sólo puedo hacerlo con los tipos de sistemas (aunque no mscorlib, un poco torpemente). pero para las enumeraciones declararon en mi App_Code, no puedo averiguar la manera de hacerlo
la AQN hace un poco de cuerda con un código divertida como esta (AstroDate es el nombre de mi clase):
"AstroDate, App_Code.rujpwg3d, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null"
pero si lo uso en el gettype, él los errores

Por favor, asesorar

Editar Aquí está el código en el control de usuario tratando de obtener una lista de los 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" es una propiedad de cadena que se pasa en el declartaion del control de usuario en la página.

¿Fue útil?

Solución 2

Aquí está el código

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 es cualquier enumeración en mi sistema, u puede utilizar cualquier u desea

Gracias a todos por tratar

Otros consejos

Yo tenía dificultades para entender lo que exactamente quería hacer. Por lo tanto, estoy tomando una conjetura:

Tiene un control de usuario que crea un DrowDownList basado en el tipo de enumeración que proporcione. Pero, que había dificultades para leer de nuevo.

I creó una muestra de trabajo que podría ser útil para usted:

<%@ 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>

Código atrás:

Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5

End Enum

Clase WebForm1 Pública     Hereda 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

Control del usuario:

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>

No estoy seguro de lo que está pasando, pero estoy teniendo problemas pegar el código. Así que, por favor, al descubierto con él y si alguien puede arreglarlo para mí!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top