網站首頁 編程語言 正文
標題可能無法表達我的本意。比如,有這樣一個枚舉:
public enum MyChoice
{
MyFirstChoice = 0,
MySecondChoice =1,
MyThirdChoice = 2
}
數據庫中,某表某字段保存值為"0,1,2",在顯示的時候,我們希望是"第一個選擇,第二個選擇,第三個選擇"。如何做呢?
可以為枚舉項上面標注自定義特性。先自定義一個特性如下:
public class EnumDisplayNameAttribute : Attribute
{
private string _displayName;
public EnumDisplayNameAttribute(string displayName)
{
_displayName = displayName;
}
public string DisplayName
{
get
{
return _displayName;
}
}
}
然后,把自定義特性標注放到枚舉項上去。
public enum MyChoice
{
[EnumDisplayName("我的第一個選擇")]
MyFirstChoice = 0,
[EnumDisplayName("我的第二個選擇")]
MySecondChoice =1,
[EnumDisplayName("我的第三個選擇")]
MyThirdChoice = 2
}
現在,需要一個幫助方法,能讀出枚舉項上的自定義特性EnumDisplayName。
public class EnumExt
{
/// <summary>
/// 獲取枚舉項的注釋
/// </summary>
/// <param name="e">枚舉項</param>
/// <returns></returns>
public static string GetEnumDescription(object e)
{
//獲取枚舉項
Type t = e.GetType();
//獲取枚舉項的字段
FieldInfo[] fis = t.GetFields();
foreach (FieldInfo fi in fis)
{
//如果當前字段名稱不是當前枚舉項
if (fi.Name != e.ToString())
{
continue;//結束本次循環
}
//如果當前字段的包含自定義特性
if (fi.IsDefined(typeof (EnumDisplayNameAttribute), true))
{
//獲取自定義特性的屬性值
return (fi.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true)[0] as EnumDisplayNameAttribute).DisplayName;
}
}
return e.ToString();
}
public static List<SelectListItem> GetSelectList(Type enumType)
{
List<SelectListItem> selectList = new List<SelectListItem>();
//selectList.Add(new SelectListItem{Text = "--請選擇--",Value = ""});
foreach (object e in Enum.GetValues(enumType))
{
selectList.Add(new SelectListItem { Text = GetEnumDescription(e), Value = ((int)e).ToString() });
}
return selectList;
}
}
以上,
GetEnumDescription方法根據枚舉項獲取其上的自定義特性EnumDisplayNameAttribute的DisplayName屬性值。
GetSelectList方法根據枚舉的Type類型返回SelectListItem集合,通常在ASP.NET MVC中使用。
最后,就能實現本篇的需求:
static void Main(string[] args)
{
string myChoiceInt = "0,1,2";
string[] choiceArr = myChoiceInt.Split(',');
string temp = string.Empty;
foreach (string item in choiceArr)
{
//轉換成枚舉的類型
short enumValShort = short.Parse(item);
temp = temp + EnumExt.GetEnumDescription((MyChoice)enumValShort) + ",";
}
Console.WriteLine(temp.Substring(0, temp.Length - 1));
Console.ReadKey();
}
原文鏈接:https://www.cnblogs.com/darrenji/p/4457328.html
相關推薦
- 2022-10-15 使用Pycharm創建一個Django項目的超詳細圖文教程_python
- 2022-05-25 SpringBoot使用Aop實現分布式鎖
- 2022-04-26 python?moviepy?的用法入門篇_python
- 2022-07-09 Docker網絡介紹
- 2022-08-06 Python中if?__name__==‘__main__‘用法詳情_python
- 2022-09-19 Golang如何編寫內存高效及CPU調優的Go結構體_Golang
- 2022-04-24 python使用技巧-查找文件?_python
- 2023-02-10 Golang中interface的基本用法詳解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支