C#
是否提供了一种使用反射从头开始创建Enum
类型的方法?
假设,我有一组字符串
:{"Single", "Married", "Divorced"}
并且我愿意构建以下枚举类型在运行时:
enum PersonStatus
{
Single, Married, Divorced
}
这有可能吗?
最佳答案
不是没有做一些非常粗糙的事情,比如使用 Emit 生成程序集。无论如何,您将如何使用这样的枚举?这里的真正目标是什么?
编辑:既然我们知道你真正想做什么,this page建议您可以使用如下代码实现您的目标:
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;
}
关于c# - 使用反射创建枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8979179/