Most efficient way to parse a flagged enum to a list

前端 未结 6 1406
猫巷女王i
猫巷女王i 2021-01-01 09:06

I have a flagged enum and need to retrieve the names of all values set on it.

I am currently taking advantage of the enum\'s ToString() method which returns the eleme

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-01 09:28

    Try this:

    public void SetRoles(Enums.Roles role)
    {
      List result = new List();
      foreach(Roles r in Enum.GetValues(typeof(Roles))
      {
        if ((role & r) != 0) result.Add(r.ToString());
      }
    }
    

提交回复
热议问题