从数值枚举字符串名称 [英] Enum String Name from Value

查看:125
本文介绍了从数值枚举字符串名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举构造是这样的:

I have an enum construct like this:

public enum EnumDisplayStatus
{
    None=1,
    Visible=2,
    Hidden=3,
    MarkedForDeletion=4
}

在我的数据库中,枚举是按值引用。我的问题是,我该如何打开再枚举的presentation回字符串名称中的数字。

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 的结果应该是可见

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();
EnumDisplayStatus enumDisplayStatus = (EnumDisplayStatus)value;
string stringValue = enumDisplayStatus.ToString();

这篇关于从数值枚举字符串名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆