我有一个枚举:
public enum MyColours
{
Red,
Green,
Blue,
Yellow,
Fuchsia,
Aqua,
Orange
}
Run Code Online (Sandbox Code Playgroud)
我有一个字符串:
string colour = "Red";
Run Code Online (Sandbox Code Playgroud)
我希望能够回归:
MyColours.Red
Run Code Online (Sandbox Code Playgroud)
从:
public MyColours GetColour(string colour)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
public MyColours GetColours(string colour)
{
string[] colours = Enum.GetNames(typeof(MyColours));
int[] values = Enum.GetValues(typeof(MyColours));
int i;
for(int i = 0; i < colours.Length; i++)
{
if(colour.Equals(colours[i], StringComparison.Ordinal)
break;
}
int value = values[i];
// I know all the information about the matched enumeration
// but how do i convert this information into returning a
// …Run Code Online (Sandbox Code Playgroud)