From 46e98339480d853fc78a014c34d7ff9fcaf13890 Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期四, 05 十二月 2024 14:09:02 +0800 Subject: [PATCH] 产线协议 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Enums/EnumHelper.cs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 111 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Enums/EnumHelper.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Enums/EnumHelper.cs" new file mode 100644 index 0000000..60cddd4 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Enums/EnumHelper.cs" @@ -0,0 +1,111 @@ +锘縰sing System.ComponentModel; +using System.Reflection; + +namespace WIDESEA_Core.Enums +{ + public static class EnumHelper + { + /// <summary> + /// 鏋氫妇杞瓧鍏搁泦鍚� + /// </summary> + /// <typeparam name="T">鏋氫妇绫诲悕绉�</typeparam> + /// <param name="keyDefault">榛樿key鍊�</param> + /// <param name="valueDefault">榛樿value鍊�</param> + /// <returns>杩斿洖鐢熸垚鐨勫瓧鍏搁泦鍚�</returns> + public static Dictionary<string, object> EnumListDic<T>(string keyDefault, string valueDefault = "") + { + Dictionary<string, object> dicEnum = new Dictionary<string, object>(); + Type enumType = typeof(T); + if (!enumType.IsEnum) + { + return dicEnum; + } + if (!string.IsNullOrEmpty(keyDefault)) //鍒ゆ柇鏄惁娣诲姞榛樿閫夐」 + { + dicEnum.Add(keyDefault, valueDefault); + } + string[] fieldstrs = Enum.GetNames(enumType); //鑾峰彇鏋氫妇瀛楁鏁扮粍 + foreach (var item in fieldstrs) + { + string description = string.Empty; + var field = enumType.GetField(item); + object[] arr = field.GetCustomAttributes(typeof(DescriptionAttribute), true); //鑾峰彇灞炴�у瓧娈垫暟缁� + if (arr != null && arr.Length > 0) + { + description = ((DescriptionAttribute)arr[0]).Description; //灞炴�ф弿杩� + } + else + { + description = item; //鎻忚堪涓嶅瓨鍦ㄥ彇瀛楁鍚嶇О + } + dicEnum.Add(description, (int)Enum.Parse(enumType, item)); //涓嶇敤鏋氫妇鐨剉alue鍊间綔涓哄瓧鍏竗ey鍊肩殑鍘熷洜浠庢灇涓句緥瀛愯兘鐪嬪嚭鏉ワ紝鍏跺疄杩欒竟搴旇鍒ゆ柇浠栫殑鍊间笉瀛樺湪锛岄粯璁ゅ彇瀛楁鍚嶇О + } + return dicEnum; + } + /// <summary> + /// 鑾峰彇鏋氫妇椤规弿杩颁俊鎭� 渚嬪GetEnumDesc(Days.Sunday) + /// </summary> + /// <param name="en">鏋氫妇椤� 濡侱ays.Sunday</param> + /// <returns></returns> + public static string GetIntegralRuleTypeEnumDesc(this Enum en) + { + Type type = en.GetType(); + MemberInfo[] memInfo = type.GetMember(en.ToString()); + if (memInfo != null && memInfo.Length > 0) + { + object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); + if (attrs != null && attrs.Length > 0) + return ((DescriptionAttribute)attrs[0]).Description; + } + return en.ToString(); + } + /// <summary> + /// 鑾峰彇鏋氫妇闆嗗悎 + /// </summary> + /// <typeparam name="T">鏋氫妇绫诲悕绉�</typeparam> + /// <returns></returns> + public static IEnumerable<EnumModel> GetEnumList<T>() + { + var model = default(T); + FieldInfo[] fieldinfo = typeof(T).GetFields(); + List<EnumModel> result = new List<EnumModel>(); + foreach (FieldInfo field in fieldinfo) + { + EnumModel enumModel = new EnumModel(); + if (!(Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) is DescriptionAttribute attribute)) + { + enumModel.Desc = field.GetValue(model).ToString(); + } + else + { + enumModel.Desc = attribute.Description; + } + enumModel.Value = field.GetValue(model).GetHashCode(); + enumModel.Key = field.GetValue(model) as ValueType; + if (field.GetValue(model).ToString() != "0") + { + result.Add(enumModel); + } + + } + return result; + } + } + + public class EnumModel + { + /// <summary> + /// Enum鐨勫�� + /// </summary> + public int Value { get; set; } + /// <summary> + /// Enum鐨刱ey + /// </summary> + public ValueType Key { get; set; } + /// <summary> + /// Enum鎻忚堪 + /// </summary> + public string Desc { get; set; } + } + +} -- Gitblit v1.9.3