0

I cant find a way to express this method into a generic method. Target is to use any flagged enum. Would be great, if someone could help.

class Program
{
  [Flags]
  public enum MyRights
  {
      Read, Write, Delete, CreateChild, FullRights
  }

  static void Main(string[] args)
  {
      MyRights myRights = new MyRights();
      myRights = MyRights.Read | MyRights.Delete;

      var kvpList = GetList(myRights);
      Console.ReadKey();
  }

  private static List<KeyValuePair<string, int>> GetList(MyRights myRights)
  {
      return Enum.GetValues(typeof(MyRights)).Cast<MyRights>()
          .Where((enumValue) => myRights.HasFlag(enumValue))
          .Select((enumValue) => 
             new KeyValuePair<string, int>(enumValue.ToString(), (int)enumValue))
          .ToList();
  }
}

Best regards Tzwenni

PS: Not regarding NULL check etc. at the moment.

UPDATE Solution

thx to @Flydog57

initialize the values

1st important hint ;)

    public enum MyRights
    {
        Read = 0x01, Write = 0x02, Delete = 0x04, CreateChild = 0x08
    }

Then: I couldn't cast value to int. But using HashCode works fine:

private static List<KeyValuePair<string, int>> GetListGeneric<TEnum>(TEnum myRights) 
        where TEnum : Enum 
{
    return Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
        .Where((enumValue) => myRights.HasFlag(enumValue))
        .Select((enumValue) =>  
            new KeyValuePair<string, int>(enumValue.ToString(), enumValue.GetHashCode())
        ).ToList();
}

Extension method

    static List<TEnum> ToList<TEnum>(this TEnum myRights) where TEnum : Enum
    {
        return Enum.GetValues(typeof(TEnum)).Cast<TEnum>().Where((enumValue) =>
            myRights.HasFlag(enumValue)).Select((enumValue) => enumValue).ToList();
    }
Tzwenni
  • 139
  • 3
  • 7
  • Have you seen https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum ? looks some useful info in there – Caius Jard Dec 09 '18 at 18:03
  • Jeap, but I couldnt find a way for me. The Select dont work then (The way I used this.. :( ) – Tzwenni Dec 09 '18 at 18:10
  • 1
    You annotate your `MyRights` enum with `[Flags]`, but you don't initialize the values. Then you use it as if it were a true Flags enum (you OR Read and Delete). You should initialize the values to 0x01, 0x02, 0x04, 0x08, 0x10, etc., i.e., each with a separate single bit set. – Flydog57 Dec 09 '18 at 18:31
  • Ok, thanks. Now I understand that there is a difference between classic use and using Flag attribute. I didnt notice since yet. Thanks ! But I cant get the point how to use then the method in generic way. – Tzwenni Dec 09 '18 at 18:40
  • Can you clarify what your method is meant to actually be doing? In particular I'm confused about what your key value pair is meant to be... – Chris Dec 09 '18 at 21:13
  • If a flagged enum is set eg, value 2 and 5, you will get back a List of Key ValuePairs where only these two are in. In practice I wont need KVP, only List. – Tzwenni Dec 09 '18 at 21:17

0 Answers0