Cá nhân tôi để nguyên chúng là số nguyên và sử dụng DescriptionAttributes và một lớp tiện ích để có được mô tả attribue cho loại. Sau đó, bạn có thể sử dụng nhiều hơn chỉ là một nhân vật để hiển thị những gì bạn muốn.
Ví dụ về điều này là;
/// <summary>
/// Returns the string value defined by the description attribute of the given enum.
/// If no description attribute is available, then it returns the string representation of the enum.
/// </summary>
/// <param name="value">Enum to use</param>
/// <returns>String representation of enum using Description attribute where possible</returns>
public static string StringValueOf(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes.Length > 0)
{
return attributes[0].Description;
}
else
{
return value.ToString();
}
}
nơi enum của bạn được định nghĩa là một cái gì đó như;
public enum Action
{
None,
[DescriptionAttribute("A")]
Address,
[DescriptionAttribute("C")]
Amendment,
[DescriptionAttribute("N")]
Normal
}
Nguồn
2012-03-07 08:58:28
"Tôi có enum char" - no you don't :) Bạn có enum 'int's, có giá trị được chỉ định bằng ký tự chữ. – porges
@Porges cảm ơn bạn đã làm rõ! – m4rc