使用反射创建枚举 [英] Create enum using reflection
本文介绍了使用反射创建枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
确实 C#
提供一种方式来创建枚举
键入从头使用反射?
Does C#
offer a way to create Enum
type from scratch using reflection?
假设,我有字符串
的集合: {单身,已婚,离婚}
,我愿意建在运行以下枚举类型的 的
Suppose, I have a collection of strings
: {"Single", "Married", "Divorced"}
and I'm willing to build the following enum type in runtime:
enum PersonStatus
{
Single, Married, Divorced
}
这是某种程度上可能吗?
Is this somehow possible?
推荐答案
不是没有做这样使用的Emit生成组件确实粗糙的东西。你会如何使用这样一个枚举呢?这里什么真正的目标。
Not without doing really gnarly things like generating assemblies using Emit. How would you use such an enum anyway? Whats the real goal here?
编辑:现在我们知道你真正想做的事情,的此页面建议您可以使用如下代码acheive你的目标:
Now that we know what you really want to do, this page suggests that you can acheive your goal using code like the following:
private void listViewComplex_CellEditStarting(object sender, CellEditEventArgs e)
{
// Ignore edit events for other columns
if (e.Column != this.columnThatYouWantToEdit)
return;
ComboBox cb = new ComboBox();
cb.Bounds = e.CellBounds;
cb.Font = ((ObjectListView)sender).Font;
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.Items.AddRange(new String[] { "Single", "Married", "Divorced" });
cb.SelectedIndex = 0; // should select the entry that reflects the current value
e.Control = cb;
}
这篇关于使用反射创建枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文