从字符串获取枚举类型

Har*_*9pl 1 c# enums types

我要做的是从字符串中获取Enum Type.

例:

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

我已经对谷歌进行了一些研究,但无法找到能满足我需要的东西.有谁在过去解决了这个问题

Joh*_*Woo 12

试试这个:

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

PS:用Colors而不是Color

以供参考