Define error category:
/// <Summary> /// error code enumeration // </Summary> Public Enum codeenum: int {resolution packet error = 10000, data packet is empty = 10001, invalid parameter = 10002, execution failed = 10003, call Interface Verification Failed = 10004, internal application error = 10005}Custom error classes:
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); // record logs} // <summary> // Business Information encoding // </Summary> Public codeenum code {Get; set ;} /// <summary> /// business information message // </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); } } }Core code:
Using system; using system. collections. generic; using system. reflection; namespace business {public class enumhelper {/// <summary> /// attribute value used to cache enumeration values /// </Summary> Private Static readonly dictionary <object, enumattribute> enumattr = new dictionary <object, enumattribute> (); // <summary> // obtain the name of the enumerated value, this name is defined by enumattribute /// </Summary> /// <Param name = "value"> name of the enumerated value </param> /// <returns> </returns> Public static string getname (Enum value) {enumattribute Ea = getattribute (value); Return ea! = NULL? EA. name: "" ;}/// <summary> // obtain the name of the enumerated value, this name is defined by enumattribute /// </Summary> /// <Param name = "value"> name of the enumerated value </param> /// <returns> </returns> Public static string getdescription (Enum value) {enumattribute Ea = getattribute (value); Return ea! = NULL? EA. description :"";} /// <summary> /// convert from string to Enumeration type /// </Summary> /// <typeparam name = "T"> Enumeration type </typeparam>/ // <Param name = "str"> string to be converted to enumeration </param> // <returns> Conversion Result </returns> Public static t getenum <t> (string str) {If (string. isnullorwhitespace (STR) throw new exceptionbiz (codeenum. an error occurred while parsing the message. "An error occurred while parsing the sorting object! "); Try {T Pe = (t) enum. parse (typeof (t), STR); Return PE;} catch {type = typeof (t ); string templete = "{1} is not defined in the enumeration type {0! "; String MSG = string. format (templete, type. tostring (), STR); throw new exceptionbiz (codeenum. parsing message error, MSG );}} /// <summary> /// get the attribute defined by the enumerated value /// </Summary> /// <Param name = "value"> enumeration object </param>/ // <returns> obtain the description attribute value of the enumerated object </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> /// description of enumeration attributes /// </Summary> [attributeusage (attributetargets. field, allowmultiple = false, inherited = true)] public class enumattribute: attribute {private string _ name; private string _ description; /// <summary> /// enumeration name /// </Summary> Public string name {get {return _ name ;}set {_ name = value ;}} /// <summary> /// enumeration description /// </Summary> Public String description {get {return _ description;} set {_ description = value ;}} /// <summary> /// constructor /// </Summary> /// <Param name = "name"> enumeration name </param> Public enumattribute (string name) {This. name = Name ;} /// <summary> /// constructor /// </Summary> /// <Param name = "name"> enumeration name </param> /// <Param name = "Description"> enumeration description </param> Public enumattribute (string name, string description) {This. name = Name; this. description = Description ;}}}Application:
Productenum Pe = enumhelper. getenum <productenum> (v. name); // obtain the enumerated object string name = enumhelper. getname (PE); // obtain the description of the enumerated object if (string. isnullorwhitespace (name) throw new exceptionbiz (codeenum. an error occurred while parsing the message. "sorting object ing error! ");
/// <Summary> // commodity sorting enumeration // </Summary> Public Enum productenum {[enumattribute ("sales")] sales volume, [enumattribute ("mprice3")] Price, [enumattribute ("Comments")] comment, [enumattribute ("ID")] latest}
Match previous sorting: http://blog.csdn.net/joyhen/article/details/39204473
You can easily process multi-dimensional sorting.
Enumeration feature tags and basic processing classes and Applications