string values enum in c# [duplicate]
there any way to define enum in c# like below?
public enum MyEnum : string
{
EnGb = "en-gb",
FaIr = "fa-ir",
...
}
ok, according to erick approach and link, i'm using this to check valid value from provided description:
public static bool IsValidDescription(string description)
{
var enumType = typeof(Culture);
foreach (Enum val in Enum.GetValues(enumType))
{
FieldInfo fi = enumType.GetField(val.ToString());
AmbientValueAttribute[] attributes = (AmbientValueAttribute[])fi.GetCustomAttributes(typeof(AmbientValueAttribute), false);
AmbientValueAttribute attr = attributes[0];
if (attr.Value.ToString() == description)
开发者_运维技巧 return true;
}
return false;
}
any improvement?
Another alternative, not efficient but giving enum functionality is to use an attribute, like this:
public enum MyEnum
{
[Description("en-gb")]
EnGb,
[Description("fa-ir")]
FaIr,
...
}
And something like an extension method, here's what I use:
public static string GetDescription<T>(this T enumerationValue) where T : struct
{
var type = enumerationValue.GetType();
if (!type.IsEnum) throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
var str = enumerationValue.ToString();
var memberInfo = type.GetMember(str);
if (memberInfo != null && memberInfo.Length > 0)
{
var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute) attrs[0]).Description;
}
return str;
}
Then you can call it like this:
MyEnum.EnGb.GetDescription()
If it has a description attribute, you get that, if it doesn't, you get the .ToString()
version, e.g. "EnGb"
. The reason I have something like this is to use an enum type directly on a Linq-to-SQL object, yet be able to show a pretty description in the UI. I'm not sure it fits your case, but throwing it out there as an option.
Regarding Matthew's answer, I suggest you to use Dictionary<MyEnum, String>
. I use it as a static property:
class MyClass
{
private static readonly IDictionary<MyEnum, String> dic = new Dictionary<MyEnum, String>
{
{ MyEnum.EnGb, "en-gb" },
{ MyEnum.RuRu, "ru-ru" },
...
};
public static IDictionary<MyEnum, String> Dic { get { return dic; } }
}
If all your names/values are going to be exactly like that, you could just create an enum as normal.
public enum MyEnum
{
EnGb
FaIr
}
And then when you need the actual value, get the enum name as a string, make it lowercase and add a -
in the middle.
string value = MyEnum.EnGb.ToString().ToLower().Insert(2, "-");
No. You can just use a Dictionary<String, String>
. The type for an enum must be an integral type other than char. This is so they can be compared efficiently.
It is only possible indirectly by using attributes on the enum values, like so:
public enum MyEnum {
[DefaultValue("en-gb")]
EnGb,
[DefaultValue("fa-ir")]
FaIr,
...
}
You can then retrieve the string value using reflection by reading the custom attributes on the static fields of the enum.
精彩评论