Defining an Extension Method for Enum in Silverlight

There is no GetValues for enumerations in Silverlight , so I thought I would write an extension method to cover my needs in my project. One thing: Im not sure what the signature of the extension method should look like. Im thinking something like:

public static IEnumerable<Enum> GetValues(this Enum e)

But it does not appear in intellisense, so I know that I am wrong. Any pointers?

+3
source share
1 answer

I think I figured this out by combining a bit of reflection with and digging into Reflector:

public static Array GetValues(this Enum enumType)
   {
       Type type = enumType.GetType();

       FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);

       Array array = Array.CreateInstance(type, fields.Length);

       for (int i = 0; i < fields.Length; i++)
       {
           var obj = fields[i].GetValue(null);
           array.SetValue(obj, i);
       }

       return array;
   }
+5
source

Source: https://habr.com/ru/post/1732206/


All Articles