uwekeim icon

Friendly Enum Strings With Flags Attribute

uwekeim | PRO | 06/20/16 12:06:01 PM UTC | 0 ⭐ | 640 👁️ | Never ⏰ | []
C# |

3.31 KB

|

None

|

0 👍

/

0 👎

/*
Based on http://stackoverflow.com/a/31839145/107625
 
It didn't work correctly with an enum like:
 
        [Flags]
        public enum Test1
        {
            [Description("Letter-A")] A = 0x00, // Special case: Zero, no flags.
            [Description("Letter-B")] B = 0x01,
            [Description("Letter-C")] C = 0x02,
            [Description("Letter-D")] D = 0x04,
            [Description("Letter-E")] E = 0x08
        }
 
My below solution (hopefully!) corrects the above false behaviour.
 
Be aware that this _only_ works for enums with [Flags] attribute. It does _not_ work for enums without
the [Flags] attribute.
 
Some unit tests:
 
        [Flags]
        public enum Test1
        {
            [D("Letter-A")] A = 0x00, // Special case: Zero, no flags.
            [D("Letter-B")] B = 0x01,
            [D("Letter-C")] C = 0x02,
            [D("Letter-D")] D = 0x04,
            [D("Letter-E")] E = 0x08
        }
 
        public enum Test2
        {
            [D("Letter-A")] A,
            [D("Letter-B")] B,
            [D("Letter-C")] C,
            [D("Letter-D")] D,
            [D("Letter-E")] E
        }
 
        [Test]
        public void TestEnum1()
        {
            const Test1 t1 = Test1.A;
            const Test1 t2 = Test1.B;
            const Test1 t3 = Test1.B | Test1.C;
 
            var d1 = StringHelper.GetEnumDescription(t1);
            var d2 = StringHelper.GetEnumDescription(t2);
            var d3 = StringHelper.GetEnumDescription(t3);
 
            Assert.AreEqual(d1, "Letter-A");
            Assert.AreEqual(d2, "Letter-B");
            Assert.AreEqual(d3, "Letter-B, Letter-C");
        }
 
        [Test]
        public void TestEnum2()
        {
            const Test2 t1 = Test2.A;
            const Test2 t2 = Test2.B;
            const Test2 t3 = Test2.C;
 
            var d1 = StringHelper.GetEnumDescription(t1);
            var d2 = StringHelper.GetEnumDescription(t2);
            var d3 = StringHelper.GetEnumDescription(t3);
 
            Assert.AreEqual(d1, "Letter-A");
            Assert.AreEqual(d2, "Letter-B");
            Assert.AreEqual(d3, "Letter-C");
        }
*/
 
public static string GetDescription(Enum value)
{
    Type type = value.GetType();
    var values = Enum.GetValues(type);
    var setValues = new List<Enum>();
    foreach(var enumValue in values)
    {
        if ((int) enumValue == 0) // Added check.
        {
            if (value.Equals(enumValue))
                setValues.Add((Enum) enumValue);
        }
        else
        {
            if (value.HasFlag((Enum) enumValue))
                setValues.Add((Enum) enumValue);
        }
    }
    var stringList = new List<string>();
    foreach (var singleValue in setValues)
    {
        var name = Enum.GetName(type, singleValue);
        if (name != null)
        {
            FieldInfo field = type.GetField(name);
            if (field != null)
            {
                DescriptionAttribute attr =
                       Attribute.GetCustomAttribute(field,
                         typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (attr != null)
                {
                    stringList.Add(attr.Description);
                }
            }
        }
    }
    return string.Join(", ", stringList.ToArray());
}

Comments