C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法
一、前言
做過系統(tǒng)參數(shù)設(shè)置的同學(xué)們,肯定遇到過要提供一系列具有相同特點的選項供用戶選擇。最初級的做法是在窗體上增加一個下拉框控件,手工填寫Items選項。然后運行時可以下拉選擇。那如果有百八十個參數(shù)都是這種方式怎么辦?
上述做法弊端很明顯。那么如何靈活的實現(xiàn)這個需求呢?
二、思路
在代碼中定義枚舉類型,然后在窗體加載時,將枚舉類型的元素(描述信息)加載到下拉框中,這樣以后增加或修改了枚舉元素后,下拉框中時刻保持的是最新的數(shù)據(jù)。再運用上反射機(jī)制,多個下拉框可以復(fù)用同一個加載方法。代碼量會大幅度減少,同時可讀性和可維護(hù)性提高了許多。
三、上代碼
1. 首先定義一個枚舉,例如:
using SharedBaseProject.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace SharedBaseProject.Common.CarPosition { public class Direction { /// <summary> /// 方向 /// </summary> public enum Enum_Direction { /// <summary> /// 默認(rèn)值 /// </summary> [Description("未指定")] None = -1, /// <summary> /// 上 /// </summary> [Description("上")] Up = 0, /// <summary> /// 下 /// </summary> [Description("下")] Down = 1, /// <summary> /// 左 /// </summary> [Description("左")] Left = 2, /// <summary> /// 右 /// </summary> [Description("右")] Right = 3 } } }
2. 引入模板方法,將枚舉轉(zhuǎn)換為List的代碼封裝為靜態(tài)方法:
using System; using System.Collections.Generic; using System.Text; namespace SharedBaseProject.CustomType { public class EnumberEntity { /// <summary> /// 枚舉的描述 /// </summary> public string Description { set; get; } /// <summary> /// 枚舉名稱 /// </summary> public string EnumName { set; get; } /// <summary> /// 枚舉對象的值 /// </summary> public int EnumValue { set; get; } } }
using SharedBaseProject.CustomType; using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; namespace SharedBaseProject.Utils { public class EnumUtil { /// <summary> /// 枚舉轉(zhuǎn)換為List /// </summary> /// <typeparam name="T"></typeparam> /// <param name="startValue">從哪個一個元素開始獲取</param> /// <returns></returns> public static List<EnumberEntity> EnumToList<T>(int startValue = 0) { List<EnumberEntity> list = new List<EnumberEntity>(); foreach (var e in Enum.GetValues(typeof(T))) { if (Convert.ToInt32(e) < startValue) { continue; } EnumberEntity m = new EnumberEntity(); object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true); if (objArr != null && objArr.Length > 0) { DescriptionAttribute da = objArr[0] as DescriptionAttribute; m.Description = da.Description; } m.EnumValue = Convert.ToInt32(e); m.EnumName = e.ToString(); list.Add(m); } return list; } } }
3. 封裝一個方法,通過反射將獲取到的List賦予下拉框。
入?yún)橄吕?、以及具體的枚舉類型:
/// <summary> /// 加載下拉框選項 /// </summary> private void loadComboBoxItems(ComboBox comboBox, Type type, int startValue = 0) { // Get the generic type definition MethodInfo method = typeof(EnumUtil).GetMethod("EnumToList", BindingFlags.Public | BindingFlags.Static); // Build a method with the specific type argument you're interested in method = method.MakeGenericMethod(type); // The "null" is because it's a static method List<EnumberEntity> listBaiduPositionMode = (List<EnumberEntity>)method.Invoke(null, new object[] { startValue }); if (listBaiduPositionMode == null || listBaiduPositionMode.Count < 1) { return; } int iCount = listBaiduPositionMode.Count; for (int i = 0; i < iCount; i++) { comboBox.Items.Add(listBaiduPositionMode.ElementAt(i).Description); } }
4. 在窗體上設(shè)置一個下拉框,命名direction:
5. 調(diào)用,參數(shù)1為下拉框控件名稱,參數(shù)2的Enum_Direction為之前定義的枚舉類型:
loadComboBoxItems(direction, typeof(Enum_Direction));
運行后效果如圖:
如果有多個類似參數(shù),每個只需一句代碼調(diào)用即可。怎么樣,是不是很方便?
到此這篇關(guān)于C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)C# 枚舉加載到ComboBo內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#對桌面應(yīng)用程序自定義鼠標(biāo)光標(biāo)
這篇文章介紹了C#對桌面應(yīng)用程序自定義鼠標(biāo)光標(biāo)的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06C# HttpClient Post參數(shù)同時上傳文件的實現(xiàn)
這篇文章主要介紹了C# HttpClient Post參數(shù)同時上傳文件的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Unity游戲開發(fā)中必備的設(shè)計模式之外觀模式詳解
外觀模式是一種結(jié)構(gòu)型設(shè)計模式,為復(fù)雜系統(tǒng)提供了簡單的接口,使得子系統(tǒng)間的通信更加簡潔和易于維護(hù)。在Unity游戲開發(fā)中,外觀模式可以幫助開發(fā)者更好地管理游戲?qū)ο蠛徒M件等復(fù)雜結(jié)構(gòu)2023-05-05