这比通过使用的扩展方法来实现枚举描述要复杂一点,就是代码多了一点,原理很简单,就是定义一个枚举类型,然后定义一个字典,字典为<枚举,字符>,然后为它赋值即可,代码如下:
namespace Entity.Commons
{
public enum Status
{
Normal = 100,
PendingApprove = 300,
Deleted = 400,
Blocked = 500,
}
/// <summary>
/// 状态描述
/// </summary>
public class StatusDescribe
{
/// <summary>
/// 状态通用字典
/// </summary>
public static Dictionary<Status, string> StatusList = new Dictionary<Status, string>
{
{Status.Normal,"正常"},
{Status.PendingApprove,"待审核"},
{Status.Deleted,"删除"},
{Status.Blocked,"禁用"},
};
}
}
调用的时间就简单了,不用在switch了,直接输出即可
Entity.Commons.StatusDescribe.StatusList[(Entity.Commons.Status)((int)i.Status)]