#464 – Getting an Enumeration’s Underlying Type at Runtime
November 29, 2011 Leave a comment
You can use the Enum.GetUnderlyingType static method to find out what the underlying type is that is being used to store an enumerated type’s enumerated values.
public enum Moods : byte { NOMOOD = 0, Ambivalent = 1, Crabby = 10, Grouchy = Crabby - 1, Happy = 42, SuperHappy = 2 * Happy } static void Main() { Type moodStorageType = Enum.GetUnderlyingType(typeof(Moods)); var min = moodStorageType.GetField("MinValue").GetValue(null); var max = moodStorageType.GetField("MaxValue").GetValue(null); Console.WriteLine("Underlying type for Moods is: {0}", moodStorageType.FullName); Console.WriteLine("Values can range from {0} to {1}", min, max); }