How to get enum value by keyname

2020-08-22 03:17发布

public enum aa{ a1=1,a2=2,a3=6,...,a100=203}

How to get value like this

string att=GetFromDatabase("attribute");    //this return a1 or a2 ...
Enum.GetValue(att);

标签: c#
3条回答
乱世女痞
2楼-- · 2020-08-22 03:42

Something like this should do the trick:

aa attEnum = (aa)Enum.Parse(typeof(aa), att);

Go to http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx for more details.

查看更多
等我变得足够好
3楼-- · 2020-08-22 03:46

Solution

string name = GetFromDatabase("attribute");
Enum.Parse(typeof(aa),name);
查看更多
老娘就宠你
4楼-- · 2020-08-22 03:51

Use Enum.Parse

string att=GetFromDatabase("attribute");    //this return a1 or a2 ...
Enum.Parse(typeof(aa), att);
查看更多
登录 后发表回答