使用反射用枚举填充下拉列表

发布于 2024-10-27 23:59:47 字数 2610 浏览 1 评论 0原文

我正在使用反射填充一个带有读取类属性的控件的页面。如果属性类型是'String',我将添加一个文本框。如果属性类型是枚举,我将添加一个下拉列表。现在我必须用枚举填充下拉选项。这怎么能做到呢?

我使用控件填充页面的枚举定义类(Assignment)和类(classOne)都位于相同的命名空间(MySolution.Data)中。当属性名称为“SkillLevel”时循环访问 classOne 属性时,我必须转到分配类,获取枚举 SkillLevelEnum 的成员并填充下拉列表。

其他下拉菜单也需要做同样的事情。

我的代码:

namespace MySolution.Data
{
  public class classOne : MyAdapter
    {
        private string _Model;

        public string Model
        {
            get { return _Model; }
            set { _Model = value; }
        }

        private Assignement.SkillLevelEnum _SkillLevel;

        public Assignement.SkillLevelEnum SkillLevel
        {
            get { return _SkillLevel; }
            set { _SkillLevel = value; }
        }

        private Assignement.MinimalSkillsEnum _MinimalSkill;

        public Assignement.MinimalSkillsEnum MinimalSkill
        {
            get { return _MinimalSkill; }
            set { _MinimalSkill = value; }
        }

        public Assignemen.WorkLoadEnum WorkLoad
        {
            get { return _WorkLoad; }
            set { _WorkLoad = value; }
        }
    }

   public class Assignement : MyAdapter
     {

        #region Enumerations

        public enum SkillLevelEnum
        {
            LowerSkills = 0, HighestSkills = 1, Any = 2
        }

        public enum MinimalSkillsEnum
        {
            Accountable = 0,
            Responsible = 1,
            Expert = 2,
            Senior = 3,
            Medium = 4,
            Junior = 5
        }

        public enum WorkLoadEnum
        {
            LessBusy = 0, MostBusy = 1, Any = 2
        }

        #endregion
   }

}

谢谢

编辑:

我不想对任何属性名称进行硬编码。我正在循环访问以下属性。

属性 = Utility.GetAllPropertyForClass("className")

面板面板 = new Panel();
panelMe.Controls.Add(面板);

foreach(属性中的PropertyInfo属性) {

        if (!property.PropertyType.IsEnum)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt" + i.ToString();                
            panel.Controls.Add(txt);  
        }
        else
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i.ToString();

            // Here based on the property.name i need to get the enum members which is defined in a different class using reflection

            panel.Controls.Add(ddl);
        }        

        panel.Controls.Add(new LiteralControl("<br/>"));
        i++;
    }    

I am populating a page with controls reading properties of a class using reflection. If the property type is 'String' I will add a text-box. If the property type is enum I am adding a dropdownlist. Now I have to populate the dropdown options with enums. How can this be done?

Both the enum definition class(Assignment) and the class(classOne) using which I am populating the page with controls are in the same Namespace(MySolution.Data). While looping through classOne properties when the property name is 'SkillLevel' I will have to go to assignment class get the members of enum SkillLevelEnum and populate the dropdown.

Same needs to be done for other dropdowns also.

My Code:

namespace MySolution.Data
{
  public class classOne : MyAdapter
    {
        private string _Model;

        public string Model
        {
            get { return _Model; }
            set { _Model = value; }
        }

        private Assignement.SkillLevelEnum _SkillLevel;

        public Assignement.SkillLevelEnum SkillLevel
        {
            get { return _SkillLevel; }
            set { _SkillLevel = value; }
        }

        private Assignement.MinimalSkillsEnum _MinimalSkill;

        public Assignement.MinimalSkillsEnum MinimalSkill
        {
            get { return _MinimalSkill; }
            set { _MinimalSkill = value; }
        }

        public Assignemen.WorkLoadEnum WorkLoad
        {
            get { return _WorkLoad; }
            set { _WorkLoad = value; }
        }
    }

   public class Assignement : MyAdapter
     {

        #region Enumerations

        public enum SkillLevelEnum
        {
            LowerSkills = 0, HighestSkills = 1, Any = 2
        }

        public enum MinimalSkillsEnum
        {
            Accountable = 0,
            Responsible = 1,
            Expert = 2,
            Senior = 3,
            Medium = 4,
            Junior = 5
        }

        public enum WorkLoadEnum
        {
            LessBusy = 0, MostBusy = 1, Any = 2
        }

        #endregion
   }

}

Thanks

Edit:

I don't want to hardcode any of the property names. I am looping through the properties as below.

properties = Utility.GetAllPropertyForClass("className")

Panel panel = new Panel();
panelMe.Controls.Add(panel);

foreach (PropertyInfo property in properties)
{

        if (!property.PropertyType.IsEnum)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt" + i.ToString();                
            panel.Controls.Add(txt);  
        }
        else
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i.ToString();

            // Here based on the property.name i need to get the enum members which is defined in a different class using reflection

            panel.Controls.Add(ddl);
        }        

        panel.Controls.Add(new LiteralControl("<br/>"));
        i++;
    }    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最初的梦 2024-11-03 23:59:47

以下是如何将枚举与下拉列表绑定的示例

public enum ProgrammingLanguage
{
    CSharp,
    VB,
    JAVA
}
foreach (ProgrammingLanguage enmLnaguage  in Enum.GetValues(typeof(ProgrammingLanguage)))
{
     cboProgrammingLanguage.Items.Add(new ListItem(enmLnaguage.ToString(), Convert.ToInt32( enmLnaguage).ToString()));
}

Here are sample examples for how to bind enum with dropdown list

public enum ProgrammingLanguage
{
    CSharp,
    VB,
    JAVA
}
foreach (ProgrammingLanguage enmLnaguage  in Enum.GetValues(typeof(ProgrammingLanguage)))
{
     cboProgrammingLanguage.Items.Add(new ListItem(enmLnaguage.ToString(), Convert.ToInt32( enmLnaguage).ToString()));
}
白龙吟 2024-11-03 23:59:47

尝试

var enumvalues=Enum.GetValues(typeof(MinimalSkillsEnum));
var enumNames=Enum.GetNames(typeof(MinimalSkillsEnum));

enumvalues 将是一个数组,而 enumNames 是一个字符串数组。

Try

var enumvalues=Enum.GetValues(typeof(MinimalSkillsEnum));
var enumNames=Enum.GetNames(typeof(MinimalSkillsEnum));

enumvalues will be an array and enumNames is a string array.

时光沙漏 2024-11-03 23:59:47

如果您的枚举是 MinimalSkillsEnum,则这应该有效:

string[] enumOptions = Enum.GetNames(typeof(MinimalSkillsEnum));

If your enum is MinimalSkillsEnum, this should work:

string[] enumOptions = Enum.GetNames(typeof(MinimalSkillsEnum));
吖咩 2024-11-03 23:59:47

您可以通过 GetNames 函数访问枚举的标签,

List<string> options = Enum.GetName(typeof(MyEnum));

然后您可以直接将其用作下拉列表的数据源

MyDropDown.DataSource = options;
MyDropDown.DataBind();

You can access the labels of an Enum via the GetNames function

List<string> options = Enum.GetName(typeof(MyEnum));

You could then use that directly as the data source for your drop-down

MyDropDown.DataSource = options;
MyDropDown.DataBind();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文