C# 如何通过键名获取枚举值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/540746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:23:18 来源:igfitidea点击:
How to get enum value by keyname
提问by ebattulga
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);
采纳答案by Bartek Szabat
Solution
解决方案
string name = GetFromDatabase("attribute");
Enum.Parse(typeof(aa),name);
回答by bruno conde
Use Enum.Parse
string att=GetFromDatabase("attribute"); //this return a1 or a2 ...
Enum.Parse(typeof(aa), att);
回答by samjudson
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.aspxfor more details.
有关更多详细信息,请访问http://msdn.microsoft.com/en-us/library/system.enum.parse.aspx。