Yip*_*Yay 4 c# reflection enums runtime
是否C#提供了使用反射从头开始创建Enum类型的方法?
假设,我有一个集合strings:{"Single", "Married", "Divorced"}我愿意在运行时构建以下枚举类型:
enum PersonStatus
{
Single, Married, Divorced
}
Run Code Online (Sandbox Code Playgroud)
这有点可能吗?
并非没有像使用Emit生成装配这样粗糙的事情.你怎么会这样使用这个枚举?什么是真正的目标?
编辑:现在我们知道你真正想做什么,这个页面表明你可以使用如下代码实现你的目标:
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;
}
Run Code Online (Sandbox Code Playgroud)
C#是否提供了一种使用反射从头开始创建枚举类型的方法?
是的,这是可能的.如果要在运行时创建类型(包括枚举),可以使用Reflection.Emit来发出实际的MSIL代码.
这是如何使用该方法实现该目标的具体示例DefineEnum.