列舉特性標記和基本處理類及應用
阿新 • • 發佈:2019-02-19
定義錯誤類別:
/// <summary>
/// 錯誤碼列舉
/// </summary>
public enum CodeEnum : int
{
解析報文出錯 = 10000,
資料包為空 = 10001,
引數無效 = 10002,
執行失敗 = 10003,
呼叫介面校驗不成功 = 10004,
應用內部錯誤 = 10005
}
自定義錯誤類:
using Newtonsoft.Json; using System; namespace Business { [JsonObject] public class ExceptionBiz : Exception { public ExceptionBiz(CodeEnum code, string msg) { Code = code; Msg = msg; LogManage.Add(StackTrace+ msg);//記錄日誌 } /// <summary> /// 業務資訊編碼 /// </summary> public CodeEnum Code { get; set; } /// <summary> /// 業務資訊訊息 /// </summary> public string Msg { get; set; } } }
核心程式碼:public static void Add(string content) { string logPath = GetApp("LogPath"); if (logPath == "") logPath = @"G:\log\"; //string dirPath = Directory.GetCurrentDirectory() + logPath; if (!Directory.Exists(logPath)) { Directory.CreateDirectory(logPath); } string fileName = DateTime.Now.ToString("yyyyMMdd")+".txt"; using (FileStream fs = new System.IO.FileStream(logPath + fileName, System.IO.FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+":"+ content); } } }
using System; using System.Collections.Generic; using System.Reflection; namespace Business { public class EnumHelper { /// <summary> /// 用於快取列舉值的屬性值 /// </summary> private static readonly Dictionary<object, EnumAttribute> enumAttr = new Dictionary<object, EnumAttribute>(); /// <summary> /// 獲取列舉值的名稱,該名稱由EnumAttribute定義 /// </summary> /// <param name="value">列舉值</param> /// <returns>列舉值對應的名稱</returns> public static string GetName(Enum value) { EnumAttribute ea = GetAttribute(value); return ea != null ? ea.Name : ""; } /// <summary> /// 獲取列舉值的名稱,該名稱由EnumAttribute定義 /// </summary> /// <param name="value">列舉值</param> /// <returns>列舉值對應的名稱</returns> public static string GetDescription(Enum value) { EnumAttribute ea = GetAttribute(value); return ea != null ? ea.Description : ""; } /// <summary> /// 從字串轉換為列舉型別 /// </summary> /// <typeparam name="T">列舉型別</typeparam> /// <param name="str">要轉為列舉的字串</param> /// <returns>轉換結果</returns> public static T GetEnum<T>(string str) { if (string.IsNullOrWhiteSpace(str)) throw new ExceptionBiz(CodeEnum.解析報文出錯, "排序物件解析出錯!"); try { T pe = (T)Enum.Parse(typeof(T), str); return pe; } catch { Type type = typeof(T); string templete = "列舉型別{0}中沒有定義{1}項!"; string msg = string.Format(templete, type.ToString(), str); throw new ExceptionBiz(CodeEnum.解析報文出錯, msg); } } /// <summary> /// 獲取列舉值定義的屬性 /// </summary> /// <param name="value">列舉物件</param> /// <returns>獲取列舉物件的描述屬性值</returns> private static EnumAttribute GetAttribute(Enum value) { if (enumAttr.ContainsKey(value)) { EnumAttribute ea = enumAttr[value]; return ea; } else { FieldInfo field = value.GetType().GetField(value.ToString()); if (field == null) return null; EnumAttribute ea = null; object[] attributes = field.GetCustomAttributes(typeof(EnumAttribute), true); if (attributes != null && attributes.Length > 0) { ea = (EnumAttribute)attributes[0]; } enumAttr[value] = ea; return ea; } } } /// <summary> ///描述列舉的屬性 /// </summary> [AttributeUsage(AttributeTargets.Field, AllowMultiple = false, Inherited = true)] public class EnumAttribute : Attribute { private string _name; private string _description; /// <summary> /// 列舉名稱 /// </summary> public string Name { get { return _name; } set { _name = value; } } /// <summary> /// 列舉描述 /// </summary> public string Description { get { return _description; } set { _description = value; } } /// <summary> /// 建構函式 /// </summary> /// <param name="name">列舉名稱</param> public EnumAttribute(string name) { this.Name = name; } /// <summary> /// 建構函式 /// </summary> /// <param name="name">列舉名稱</param> /// <param name="description">列舉描述</param> public EnumAttribute(string name, string description) { this.Name = name; this.Description = description; } } }
應用:
ProductEnum pe = EnumHelper.GetEnum<ProductEnum>(v.Name); //名稱獲取列舉物件
string name = EnumHelper.GetName(pe); //獲取列舉物件的描述
if (string.IsNullOrWhiteSpace(name))
throw new ExceptionBiz(CodeEnum.解析報文出錯, "排序物件映射出錯!");
/// <summary>
/// 商品排序列舉
/// </summary>
public enum ProductEnum
{
[EnumAttribute("sales")]銷量,
[EnumAttribute("mprice3")]價格,
[EnumAttribute("comments")]評論,
[EnumAttribute("id")]最新
}
配合之前一篇排序處理:http://blog.csdn.net/joyhen/article/details/39204473
可以很方便的處理多維度排序