Possible to have strings for enums?
.net, c#, enums
Solution
using System.ComponentModel;
enum FilterType
{
[Description("Rigid")]
Rigid,
[Description("Soft / Glow")]
SoftGlow,
[Description("Ghost")]
Ghost ,
}
You can get the value out like this
public static String GetEnumerationDescription(Enum e)
{
Type type = e.GetType();
FieldInfo fieldInfo = type.GetField(e.ToString());
DescriptionAttribute[] da = (DescriptionAttribute[])(fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false));
if (da.Length > 0)
{
return da[0].Description;
}
return e.ToString();
}
Problem
I want to have an enum as in: ``` enum FilterType { Rigid = "Rigid", SoftGlow = "Soft / Glow", Ghost = "Ghost", } ``` How to achieve this? Is there a better way to do this? It's gonna be used for an instance of an object where it's gonna be serialized/deserialized. It's also gonna populate a dropdownlist.