Original: Converts a numeric string in a database about an enumerated item value into a literal string
The title may not express what I meant. For example, there is an enumeration:
public enum MyChoice
{
Myfirstchoice = 0,
Mysecondchoice = 1,
Mythirdchoice = 2
}
In the database, a table of a field holds the value "0,1,2", when displayed, we want to be "first choice, second choice, third choice". How to do it?
You can annotate a custom attribute above an enumeration item. First, customize one of the following features:
public class Enumdisplaynameattribute:attribute
{
private string _displayname;
Public Enumdisplaynameattribute (String displayName)
{
_displayname = DisplayName;
}
public string DisplayName
{
Get
{
return _displayname;
}
}
}
Then, you put the custom attribute callout on the enumeration item.
public enum MyChoice
{
[Enumdisplayname ("My First Choice")]
Myfirstchoice = 0,
[Enumdisplayname ("My second choice")]
Mysecondchoice = 1,
[Enumdisplayname ("My third choice")]
Mythirdchoice = 2
}
Now, a helper method is required to read out the custom attribute Enumdisplayname on the enumeration.
public class Enumext
{
< Summary >
Gets the comment for the enumeration item
</ Summary >
< param name="E"> enum entry </param>
< returns > </ returns >
public static string Getenumdescription (Object e)
{
Get Enumeration Entries
Type t = E.gettype ();
Get the fields of an enumerated item
fieldinfo[] fis = T.getfields ();
foreach (FieldInfo fi in FIS)
{
If the current field name is not the current enumeration item
if (FI. Name! = e.tostring ())
{
continue;//end of this cycle
}
If the current field contains custom attributes
if (FI. IsDefined (typeof (Enumdisplaynameattribute), true))
{
Get property values for custom attributes
Return (FI. GetCustomAttributes (typeof (Enumdisplaynameattribute), true) [0] as Enumdisplaynameattribute). DisplayName;
}
}
return e.tostring ();
}
public static List<SelectListItem> getselectlist (Type enumtype)
{
List<SelectListItem> selectlist = new List<SelectListItem > ();
Selectlist.add (New Selectlistitem{text = "---Please select--", Value = "}");
foreach (Object E in Enum.getvalues (enumtype))
{
Selectlist.add (new SelectListItem {Text = Getenumdescription (e), Value = ((int) e). ToString ()});
}
return selectlist;
}
}
Above
The Getenumdescription method obtains the DisplayName property value of the custom attribute Enumdisplaynameattribute on its own based on the enumeration item.
The Getselectlist method returns the SelectListItem collection based on the type of the enumeration, which is typically used in ASP.
Finally, you can achieve the requirements of this article:
static void Main (string[] args)
{
String mychoiceint = "0,1,2";
string[] Choicearr = Mychoiceint.split (', ');
String temp = string. Empty;
foreach (string item in Choicearr)
{
Types converted to enumerations
Short Enumvalshort = short. Parse (item);
Temp = temp + enumext.getenumdescription ((MyChoice) enumvalshort) + ",";
}
Console.WriteLine (temp. Substring (0, temp. LENGTH-1));
Console.readkey ();
}
Converts a numeric string in a database about an enumerated item value into a literal string