這比通過使用的擴充套件方法來實現列舉描述要複雜一點,就是程式碼多了一點,原理很簡單,就是定義一個列舉型別,然後定義一個字典,字典為<列舉,字元>,然後為它賦值即可,程式碼如下:
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)]