Created
April 7, 2014 08:26
-
-
Save tdewilde/10016625 to your computer and use it in GitHub Desktop.
Convert an Enum to a Dictionary<int, string>. Uses the extension method Enum.GetDescription() to retrieve the value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Dictionary<int, string> GetEnumDictionary<T>() where T : struct | |
| { | |
| if (!typeof(T).IsEnum) | |
| throw new ArgumentException("T is not an Enum type"); | |
| return Enum.GetValues(typeof(T)) | |
| .Cast<object>() | |
| .ToDictionary(k => (int)k, v => ((Enum)v).GetDescription()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good ! It is better if you try to make it either in int or short or byte means datatype independent