Iterating through an enumeration in Silverlight?

前端 未结 4 1959
孤街浪徒
孤街浪徒 2020-12-03 16:53

In .Net it is possible to iterate through an enumeration by using

System.Enum.GetNames(typeof(MyEnum)) 

or

System.Enum.Ge         


        
相关标签:
4条回答
  • 2020-12-03 17:10

    I haven't tried this, but the reflection APIs should work.

    0 讨论(0)
  • 2020-12-03 17:18

    I belive this is the same as in the .NET Compact Framework. If we make the assumption that your enum values start at 0 and use every value until their range is over the following code should work.

    public static IList<int> GetEnumValues(Type oEnumType)
    {
      int iLoop = 0;
      bool bDefined = true;
      List<int> oList = new List<int>();
    
      //Loop values
      do
      {
        //Check if the value is defined
        if (Enum.IsDefined(oEnumType, iLoop))
        {
          //Add item to the value list and increment
          oList.Add(iLoop);
          ++iLoop;
        }
        else
        {
          //Set undefined
          bDefined = false;
        }
      } while (bDefined);
    
      //Return the list
      return oList;
    }
    

    Obviously you could tweak the code to return the enum names or to match diferent patterns e.g. bitwise values.

    Here is an alternate version of the method that returns a IList<EnumType>.

    public static IList<T> GetEnumValues<T>()
    {
      Type oEnumType;
      int iLoop = 0;  
      bool bDefined = true;  
      List<T> oList = new List<T>();  
    
      //Get the enum type
      oEnumType = typeof(T);
    
      //Check that we have an enum
      if (oEnumType.IsEnum)
      {
        //Loop values  
        do
        {
          //Check if the value is defined    
          if (Enum.IsDefined(oEnumType, iLoop))
          {
            //Add item to the value list and increment      
            oList.Add((T) (object) iLoop);
            ++iLoop;
          }
          else
          {
            //Set undefined      
            bDefined = false;
          }
        } while (bDefined);
      }
    
      //Return the list  
      return oList;
    }
    
    0 讨论(0)
  • 2020-12-03 17:29

    I figured out how to do this without making assumptions about the enum, mimicking the functions in .Net:

    public static string[] GetNames(this Enum e) {
        List<string> enumNames = new List<string>();
    
        foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)){
            enumNames.Add(fi.Name);
        }
    
        return enumNames.ToArray<string>();
    }
    
    public static Array GetValues(this Enum e) {
        List<int> enumValues = new List<int>();
    
        foreach (FieldInfo fi in e.GetType().GetFields(BindingFlags.Static | BindingFlags.Public)) {
            enumValues.Add((int)Enum.Parse(e.GetType(), fi.Name, false));
        }
    
        return enumValues.ToArray();
    }
    
    0 讨论(0)
  • 2020-12-03 17:34

    Or maybe strongly typed using linq, like this:

        public static T[] GetEnumValues<T>()
        {
            var type = typeof(T);
            if (!type.IsEnum)
                throw new ArgumentException("Type '" + type.Name + "' is not an enum");
    
            return (
              from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
              where field.IsLiteral
              select (T)field.GetValue(null)
            ).ToArray();
        }
    
        public static string[] GetEnumStrings<T>()
        {
            var type = typeof(T);
            if (!type.IsEnum)
                throw new ArgumentException("Type '" + type.Name + "' is not an enum");
    
            return (
              from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
              where field.IsLiteral
              select field.Name
            ).ToArray();
        }
    
    0 讨论(0)
提交回复
热议问题