1

What I'm trying to do is get Enum Type from string.

example:

//enum which can be changed I'll know only string name of it at runtime ("Color")
public enum Color
{ 
   Black,
   Green,
   Yellow
}

I've maked some research on google but was unable to find something which do what i need. Anyone resolved this in past?

1
  • could you provide more code what are you trying to do. Commented Jun 18, 2012 at 8:10

2 Answers 2

12

Try this:

Color c = (Color) Enum.Parse(typeof(Color), "Yellow", true);
Console.WriteLine("Color Value: {0}", c.ToString());

PS: use Colors instead of Color

FOR REFERENCE

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

2 Comments

As he stated in question, his problem is that he does not know Color at compile-time
like archil said I don't know Color at runtime, but thanks for trying to help:)
4

You will need Type.GetType method

public static Type GetType(
    string typeName
)

1 Comment

Thanks:) I forgot about that method :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.