2

我有一个代码,它给出了任何给定枚举的所有可能值的
列表
值列表
,因为我的子需要一个类型参数

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

我试图将用户控件声明中使用的字符串转换为一种类型。问题是我只能使用系统类型(即使是非 mscorlib,有点笨拙)。但是对于在我的 app_code 中声明的枚举,我无法弄清楚如何做到这一点
,aqn 用这样一个有趣的代码创建了一些字符串(AstroDate 是我的班级的名称):
“AstroDate, App_Code.rujpwg3d, Version=0.0.0.0 , Culture=neutral, PublicKeyToken=null"
但如果我在 gettype 中使用它,它会出错

请指教

编辑 这里是试图获取枚举列表的用户控件中的代码

    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”是在页面中用户控件的声明中传递的字符串属性。

4

2 回答 2

1

我很难理解你到底想做什么。所以,我在猜测:

您有一个 UserControl,它根据您提供的枚举类型创建 DrowDownList。但是,你很难读回来。

我创建了一个可能对您有帮助的工作示例:

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

后面的代码:

Public Enum TestEnum
Value1
Value2
Value3
Value4
Value5

结束枚举

公共类 WebForm1 继承 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

结束类

用户控制:

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

结束类

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="DynamicComboFromEnum.ascx.vb" Inherits="WebApplication2.DynamicComboFromEnum" %>

<asp:DropDownList ID="MainList" runat="server"></asp:DropDownList>

我不确定发生了什么,但我在粘贴代码时遇到问题。所以,请忍受它,如果有人可以为我修好它!

于 2010-06-23T18:01:25.690 回答
0

这是代码

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 只是我系统中的任何枚举,你可以使用任何你想要的

谢谢大家的尝试

于 2010-11-29T15:49:08.450 回答