从值枚举字符串名称 [英] Enum String Name from Value
本文介绍了从值枚举字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的枚举结构:
I have an enum construct like this:
public enum EnumDisplayStatus
{
None = 1,
Visible = 2,
Hidden = 3,
MarkedForDeletion = 4
}
在我的数据库中,枚举是按值引用的.我的问题是,如何将枚举的数字表示形式转回字符串名称.
In my database, the enumerations are referenced by value. My question is, how can I turn the number representation of the enum back to the string name.
例如,给定 2
结果应该是 Visible
.
For example, given 2
the result should be Visible
.
推荐答案
您可以通过简单的强制转换将 int
转换回枚举成员,然后调用 ToString()代码>:
You can convert the int
back to an enumeration member with a simple cast, and then call ToString()
:
int value = GetValueFromDb();
var enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();
这篇关于从值枚举字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文