5

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?

5
  • 7
    enumis a contract, what is the meaning of creating it in runtime? Commented Jan 23, 2012 at 21:59
  • @gdoron enums can be interpretted and used at runtime, so it's certainly not impossible for there to be value in emitting them a runtime, though Chris' answer hits on what's really useful in this case. Commented Jan 23, 2012 at 22:20
  • @JonHanna. Please show me one scenario where it's needed, because in this scenario it's NOT! Commented Feb 5, 2012 at 2:34
  • @gdoron Why should I? I've said it can be done, and I've said it's not needed here. Commented Feb 6, 2012 at 11:19
  • An enum is used if you have an object that has only a limited number of values, especially if the number of values is fairly small. The nice thing about using an enum is that at compile time it is already checked that you don't assign an invalid value to an object of the enum type. Suppose you have some hardware that has a parameter with a limited number of values that you can only detect at runtime. Using enumbuilder you can check at compile time that no invalid value will be assigned at runtime, because you can only assign values you got from the enumtype as allowed values Commented Nov 14, 2014 at 10:11

2 Answers 2

9

Does C# offer a way to create Enum type from scratch using reflection?

Yes, it's possible. If you want to create types (including enums) at runtime you could use Reflection.Emit in order to emit the actual MSIL code.

Here's a concrete example of how to achieve that using the DefineEnum method.

Sign up to request clarification or add additional context in comments.

7 Comments

What's Reflection.Emit if not standaard reflection?
Not that it's what the OP actually needed, but there's always the next person coming in on a google search :)
Emit is about code injection, reflection is about available data manipulation too. They are different.
@JonHanna, what makes you think that this is not the code that the OP needed? What makes you think that the only way to respond to the sentence Suppose, I have a collection of strings: {"Single", "Married", "Divorced"} and I'm willing to build the following enum type in runtime: is to use reflection emit and provide a specific example of how to do that? Of course feel free to downvote my answer if you think that it is wrong and in addition to that you might provide your answer that you think would be correct. Sorry to having to say this but your behavior really pisses me off.
@JonHanna, sorry if I misunderstood your comment. My English is very bad. I completely agreed with your first comment when you said that Reflection.Emit is actually Reflection and that's why I updated my answer. Sorry once again for my confusion. I feel I completely misunderstood your comment about Google. Language barrier :-)
|
8

Not without doing really gnarly things like generating assemblies using Emit. How would you use such an enum anyway? Whats the real goal here?

EDIT: 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;
}

2 Comments

@Well, I'm using ObjectListView control, which can automatically display useful editing hints when the value is a enum. I thought that if there is a simple way to generate them programmatically, it would be a nice solution.
Thank you, I've already implemented something similiar (dropping combobox) myself. Also the Reflection.Emit seems to be a nice feature to know about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.