I know How to find enum value if enum name & key are known at compile time. I have a situation where I get the enum name at runtime. any suggestion how to achieve this.
using System;
namespace EnumDemo
{
internal class Program
{
private static void Main(string[] args)
{
string[] ArrItemNames = Enum.GetNames(typeof (EnumClass.Colors));
foreach (string ItemName in ArrItemNames)
{
Console.WriteLine(
"{0} = {1:D}", ItemName,
Enum.Parse(typeof (EnumClass.Colors), ItemName));
}
Console.WriteLine();
var EnumVal = GetEnumValue("Colors", "Red");// Here I am expecting 1
Console.ReadKey();
}
//
public static int GetEnumValue(string EnumName, string ItemName)
{
return 0;
}
}
public class EnumClass
{
public enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
}
}
Note: My enum is inside a class.