How to get enum value by keyname

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

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); 

回答1:

Solution

string name = GetFromDatabase("attribute"); Enum.Parse(typeof(aa),name); 


回答2:

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:

Use Enum.Parse

string att=GetFromDatabase("attribute");    //this return a1 or a2 ... Enum.Parse(typeof(aa), att); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!