Advertisement
uwekeim

Friendly Enum Strings With Flags Attribute

Jun 20th, 2016
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. /*
  2. Based on http://stackoverflow.com/a/31839145/107625
  3.  
  4. It didn't work correctly with an enum like:
  5.  
  6.         [Flags]
  7.         public enum Test1
  8.         {
  9.             [Description("Letter-A")] A = 0x00, // Special case: Zero, no flags.
  10.             [Description("Letter-B")] B = 0x01,
  11.             [Description("Letter-C")] C = 0x02,
  12.             [Description("Letter-D")] D = 0x04,
  13.             [Description("Letter-E")] E = 0x08
  14.         }
  15.  
  16. My below solution (hopefully!) corrects the above false behaviour.
  17.  
  18. Be aware that this _only_ works for enums with [Flags] attribute. It does _not_ work for enums without
  19. the [Flags] attribute.
  20.  
  21. Some unit tests:
  22.  
  23.         [Flags]
  24.         public enum Test1
  25.         {
  26.             [D("Letter-A")] A = 0x00, // Special case: Zero, no flags.
  27.             [D("Letter-B")] B = 0x01,
  28.             [D("Letter-C")] C = 0x02,
  29.             [D("Letter-D")] D = 0x04,
  30.             [D("Letter-E")] E = 0x08
  31.         }
  32.  
  33.         public enum Test2
  34.         {
  35.             [D("Letter-A")] A,
  36.             [D("Letter-B")] B,
  37.             [D("Letter-C")] C,
  38.             [D("Letter-D")] D,
  39.             [D("Letter-E")] E
  40.         }
  41.  
  42.         [Test]
  43.         public void TestEnum1()
  44.         {
  45.             const Test1 t1 = Test1.A;
  46.             const Test1 t2 = Test1.B;
  47.             const Test1 t3 = Test1.B | Test1.C;
  48.  
  49.             var d1 = StringHelper.GetEnumDescription(t1);
  50.             var d2 = StringHelper.GetEnumDescription(t2);
  51.             var d3 = StringHelper.GetEnumDescription(t3);
  52.  
  53.             Assert.AreEqual(d1, "Letter-A");
  54.             Assert.AreEqual(d2, "Letter-B");
  55.             Assert.AreEqual(d3, "Letter-B, Letter-C");
  56.         }
  57.  
  58.         [Test]
  59.         public void TestEnum2()
  60.         {
  61.             const Test2 t1 = Test2.A;
  62.             const Test2 t2 = Test2.B;
  63.             const Test2 t3 = Test2.C;
  64.  
  65.             var d1 = StringHelper.GetEnumDescription(t1);
  66.             var d2 = StringHelper.GetEnumDescription(t2);
  67.             var d3 = StringHelper.GetEnumDescription(t3);
  68.  
  69.             Assert.AreEqual(d1, "Letter-A");
  70.             Assert.AreEqual(d2, "Letter-B");
  71.             Assert.AreEqual(d3, "Letter-C");
  72.         }
  73. */
  74.  
  75. public static string GetDescription(Enum value)
  76. {
  77.     Type type = value.GetType();
  78.     var values = Enum.GetValues(type);
  79.     var setValues = new List<Enum>();
  80.     foreach(var enumValue in values)
  81.     {
  82.         if ((int) enumValue == 0) // Added check.
  83.         {
  84.             if (value.Equals(enumValue))
  85.                 setValues.Add((Enum) enumValue);
  86.         }
  87.         else
  88.         {
  89.             if (value.HasFlag((Enum) enumValue))
  90.                 setValues.Add((Enum) enumValue);
  91.         }
  92.     }
  93.     var stringList = new List<string>();
  94.     foreach (var singleValue in setValues)
  95.     {
  96.         var name = Enum.GetName(type, singleValue);
  97.         if (name != null)
  98.         {
  99.             FieldInfo field = type.GetField(name);
  100.             if (field != null)
  101.             {
  102.                 DescriptionAttribute attr =
  103.                        Attribute.GetCustomAttribute(field,
  104.                          typeof(DescriptionAttribute)) as DescriptionAttribute;
  105.                 if (attr != null)
  106.                 {
  107.                     stringList.Add(attr.Description);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.     return string.Join(", ", stringList.ToArray());
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement