2011-03-14 14 views
13

आप एक enum के अंतर्निहित/व्युत्पन्न प्रकार (बाइट, लघु, int, आदि) कैसे प्राप्त कर सकते हैं?अंतर्निहित/व्युत्पन्न प्रकार के enum प्राप्त करें?

+1

@ChrisF: प्रकार मूल्य नहीं के लिए देख रहे हैं। – Will

+0

मेरी माफ़ी। मैंने दूसरे खिताब को गलत तरीके से पढ़ा। अगर मैं करीबी वोट वापस ले सकता हूं तो मैं चाहता हूं। हटाए गए ऑटो डाले गए टिप्पणी। – ChrisF

उत्तर

19

आप Enum.GetUnderlyingType(enumType) की तलाश में हैं; MSDN से

नमूना:

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

    Type undertype = Enum.GetUnderlyingType(entype); 

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

बस पर्याप्त, धन्यवाद। – 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(); 
    }  
} 
संबंधित मुद्दे