2011-03-14 30 views
13

¿Cómo se puede obtener el tipo subyacente/derivado (byte, short, int, etc.) de una enumeración?Obtener tipo subyacente/derivado de enum?

+1

@ChrisF: buscando el tipo no el valor. – Will

+0

Mis disculpas. Leí mal el otro título. Si pudiera retirar el voto cercano, lo haría. Se eliminó el comentario insertado automáticamente. – ChrisF

Respuesta

19

Está buscando Enum.GetUnderlyingType(enumType);

muestra de la MSDN:

static object GetAsUnderlyingType(Enum enval) 
{ 
    Type entype = enval.GetType(); 

    Type undertype = Enum.GetUnderlyingType(entype); 

    return Convert.ChangeType(enval, undertype); 
} 
+0

Simplemente lo suficiente, gracias. – Will

3
using System; 

class Program 
{ 
    enum IntEnum : int { A } 

    static void Main(string[] args) 
    { 
     var intEnum = IntEnum.A; 

     Console.WriteLine(intEnum.GetType().GetEnumUnderlyingType()); 

     Console.WriteLine("Press any key to exit..."); 
     Console.ReadKey(); 
    }  
}