In actual development, in database table design, we are often used to using an int type state field to represent the data status. This field is very convenient to represent the status of this data, however, it is unwilling to create a foreign key table for this state field to explain the state. (There may be many fields in the status of this type of table. Here is an example)
We generally treat this state field as a convention to apply it in the project (for example, 0: enabled, 1: disabled)
When the actual status of the int type is displayed in the background management or other places, write a method to the public class, and use a switch... case to return the corresponding Chinese explanation.
Http://www.dtan.so
However, I am used to using an Enum enumeration to standardize all the State fields in the database. The use of Enum is also more conducive to development. You can separately comment on enumeration and the conventions can be presented to developers, instead of simply making an appointment out of thin air. The following describes how to use the enum class.
1. First, we can create an entity class for the enumeration type: readenum
public class ReadEnum{ public string Name { get; set; } public int Value { get; set; }}
2. Step 2: Create an enumeration corresponding to the state field
# Region # state enumeration (all State enumeration in the database) /// <summary> // state enumeration (all State enumeration in the database) /// Creator: porschev // Creation Time: 2011-7-19 /// </Summary> Public Enum ssnstate {// <summary> /// enable /// </Summary> [description ("enable ")] enabled = 0, // <summary> // disable /// </Summary> [description ("disabled")] disable = 1} # endregion
As shown in the enumeration created above, developers generally do not use the description attribute in red when using enumeration. It is in the namespace of system. componentmodel.
With this, we do not need to use the switch... Case Method to interpret or display Chinese.
Step 3: write some application methods for all Enum
# Region # obtain the enum type description /// <summary> /// obtain the enum type description /// Creation Time: 2011-7-19 /// </Summary> /// <Param name = "enumtype"> Enumeration type </param> /// <Param name = "Val"> enumeration value </param> // <returns> string </returns> Public static string getenumdesc (type enumtype, object Val) {string enumvalue = system. enum. getname (enumtype, Val); If (string. isnullorempty (enumvalue) {return "";} system. reflection. fieldinfo fin Fo = enumtype. getfield (enumvalue); object [] enumattr = finfo. getcustomattributes (typeof (system. componentmodel. descriptionattribute), true); If (enumattr. length> 0) {system. componentmodel. descriptionattribute DESC = enumattr [0] As system. componentmodel. descriptionattribute; If (DESC! = NULL) {return DESC. description ;}} return enumvalue ;} # endregion # region # obtain all information about an enumeration /// <summary> /// obtain all information about an enumeration // creation time: 2011-7-19 /// </Summary> /// <typeparam name = "T"> enumeration </typeparam> /// <returns> enumerate all information </returns> Public static list <readenum> getenumlist <t> () {list <readenum> List = new list <readenum> (); readenum Re = NULL; type = typeof (t); foreach (INT enu in system. enum. getvalues (typeof (t) {Re = new readenum (); Re. name = getenumdesc (type, enu); Re. value = enu; List. add (re);} return list ;} # endregion # region # Return the enumerated content based on the value /// <summary> /// return the enumerated content based on the value /// creation time: 2011-7-19 /// </Summary> /// <typeparam name = "T"> enumeration </typeparam> /// <Param name = "value"> value (INT) </param> // <returns> </returns> Public static t GetModel <t> (INT value) {T myenum = (t) system. enum. parse (typeof (t), value. tostring (), true); Return myenum ;} # endregion # region # Return the enumerated content based on the value /// <summary> /// return the enumerated content based on the value /// creation time: 2011-7-19 /// </Summary> /// <typeparam name = "T"> enumeration </typeparam> /// <Param name = "value"> value (string) </param> // <returns> </returns> Public static t GetModel <t> (string value) {T myenum = (t) system. enum. parse (typeof (t), value, true); Return myenum ;}# endregion
These methods can fully meet the needs of Enum enumeration in the project.
Step 4: Test code
String STR = getenumdesc (typeof (ssnstate), 0); // result: List <readenum> List = getenumlist <ssnstate> () is enabled; // result: list. count = 2 // The first element: Name: enabled; Value: 0 // The second element: Name: Disabled; Value: 1 ssnstate Re = GetModel <ssnstate> (0 ); // result: ssnstate. enabled ssnstate RE1 = GetModel <ssnstate> ("0"); // result: ssnstate. enabled
Use the enum Enumeration type during development