We have the following enum:
public enum TrafficLight
{
[Description("../../images/dot-red.png")]
[AwesomeIcon("<i class='fa fa-lg fa-circle v4-red'></i>")]
Red = 0,
[Description("../../images/dot-yellow.png")]
[AwesomeIcon("<i class='fa fa-lg fa-circle v4-yellow'></i>")]
Yellow = 1,
[Description("../../images/dot-green.png")]
[AwesomeIcon("<i class='fa fa-lg fa-circle v4-green'></i>")]
Green = 2,
[Description("../../images/dot-gold.png")]
[AwesomeIcon("<i class='fa fa-lg fa-circle v4-gold'></i>")]
Gold = 3,
[Description("../../images/dot-grey.png")]
[AwesomeIcon("<i class='fa fa-lg fa-circle v4-grey'></i>")]
Grey = 99
}
AwesomeIcon inherits from the DescriptionAttribute class:
public class AwesomeIcon : DescriptionAttribute
{
public AwesomeIcon(string icon)
: base(icon)
{
}
}
The problem is when the Enum description is accessed more than once, the order of the attributes changes. For example, we're getting the description like so:
public static string GetEnumDescription(Enum value, int index = 0)
{
var fi = value.GetType().GetField(value.ToString());
var attributes =
fi.GetCustomAttributes(typeof (DescriptionAttribute), false) as DescriptionAttribute[];
if(attributes == null)
{
return string.Empty;
}
if (attributes.Any() && index <= attributes.Length)
{
return attributes[index].Description;
}
return value.ToString();
}
First Access
GetEnumDescription(TrafficLight.Red);
//Returns "../../images/dot-red.png" as expected.
//Debug GetEnumDescription attributes variable
[0]Description
[1]AwesomeIcon
Second Access
GetEnumDescription(TrafficLight.Yellow);
//Returns "<i class='fa fa-lg fa-circle v4-yellow'></i>"
// which is the value of AwesomeIcon.
//Debug GetEnumDescription attributes variable
[0]AwesomeIcon
[1]Description
Every access after the order remains AwesomeIcon, then Description.
The problem is that AwesomeIcon inherits from Description and it's getting picked up. If the order remains the same in the array it wouldn't be a problem, I can reference it by index.
I've not experienced this before, any ideas?