No, but you can do this instead:
public enum SoftwareEmployee {
[Description("Team Manager")] TeamManager = 1,
[Description("Team Leader")] TeamLeader = 2,
[Description("Senior Developer")] SeniorDeveloper = 3,
[Description("Junior")] Junior = 4
}
You can then use a utility method to translate enum values to descriptions, for example:
/// <summary>
/// Given an enum value, if a <see cref="DescriptionAttribute"/> attribute has been defined on it, then return that.
/// Otherwise return the enum name.
/// </summary>
/// <typeparam name="T">Enum type to look in</typeparam>
/// <param name="value">Enum value</param>
/// <returns>Description or name</returns>
public static string ToDescription<T>(this T value) where T : struct {
if(!typeof(T).IsEnum) {
throw new ArgumentException(Properties.Resources.TypeIsNotAnEnum, "T");
}
var fieldName = Enum.GetName(typeof(T), value);
if(fieldName == null) {
return string.Empty;
}
var fieldInfo = typeof(T).GetField(fieldName, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
if(fieldInfo == null) {
return string.Empty;
}
var descriptionAttribute = (DescriptionAttribute) fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();
if(descriptionAttribute == null) {
return fieldInfo.Name;
}
return descriptionAttribute.Description;
}
I prefer this over manual translation via switch
, because it is easier to maintain the enum definitions if everything is together.
To allow localisation of the description text, use a different description attribute that takes its value from a resource, e.g. ResourceDescription. As long as it inherits from Description, then it will work fine. For example:
public enum SoftwareEmployee {
[ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamManager)] TeamManager = 1,
[ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.TeamLeader)] TeamLeader = 2,
[ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.SeniorDeveloper)] SeniorDeveloper = 3,
[ResourceDescription(typeof(SoftwareEmployee), Properties.Resources.Junior)] Junior = 4
}