Enum to dictionary

前端 未结 5 1376
南方客
南方客 2020-12-15 16:37

I want to implement an extension method which converts an enum to a dictionary:

public static Dictionary ToDictionary(this Enum @enum)
{
           


        
5条回答
  •  我在风中等你
    2020-12-15 16:52

    Jon Skeet has written everything you need ;)

    But here you have your code that is working:

    public static Dictionary ToDictionary(this Enum @enum)
    {
      var type = @enum.GetType();
      return Enum.GetValues(type).Cast().ToDictionary(e => e, e => Enum.GetName(type, e));
    }
    

提交回复
热议问题