2012-01-13 93 views
1
static void Main(string[] args) 
    { 
     List<string> tests = new List<string>() { "TypeTester.NonExist", "TypeTester.Thing", "TypeTester.Fruit", "TypeTester.Color" }; 
     PrintEnums(); 
     foreach (var item in tests) 
     { 
      try 
      { 
       ConvertFromTypeName(item); 
      } 
      catch (TypeLoadException) 
      { 
       Console.WriteLine("Could not load {0}", item); 
      } 
      catch (ArgumentException e) 
      { 
       Console.WriteLine(e.Message); 
      }  
     } 
     Console.ReadLine(); 
    } 

    static void PrintEnums() 
    { 
     Console.WriteLine("Printing Fruit"); 
     foreach (Fruit thing in Enum.GetValues(typeof(Fruit))) 
     { 
      Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription()); 
     } 
     Console.WriteLine("Printing Color"); 
     foreach (Color thing in Enum.GetValues(typeof(Color))) 
     { 
      Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription()); 
     } 
    } 

    static void ConvertFromTypeName(string typeName) 
    { 
     Type type = Type.GetType(typeName, true); 
     if (!type.IsEnum) 
      throw new ArgumentException(typeName + " is not an enum."); 

     Console.WriteLine("UnderlingType: {0}", type.UnderlyingSystemType); 
     string description = string.Empty; 
     foreach (var thing in Enum.GetValues((type.UnderlyingSystemType))) 
      { 
//This will not compile     
//Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription()); 
       Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.ToString()); 
     } 
    } 
} 

public class Thing 
{ 
    public int Id { get; set; } 
    public string MyName { get; set; } 
} 

public enum Fruit 
{ 
    [System.ComponentModel.DescriptionAttribute("I am an apple")] 
    Apple = 8, 
    [System.ComponentModel.DescriptionAttribute("I am an Banana")] 
    Banana, 
    [System.ComponentModel.DescriptionAttribute("I am an Grape")] 
    Grape, 
    [System.ComponentModel.DescriptionAttribute("I am an Kiwi")] 
    Kiwi, 
    [System.ComponentModel.DescriptionAttribute("I am an Orange")] 
    Orange 
} 

public enum Color 
{ 
    [System.ComponentModel.DescriptionAttribute("I am the color Red")] 
    Red = 56, 
    [System.ComponentModel.DescriptionAttribute("I am the color Green")] 
    Green, 
    [System.ComponentModel.DescriptionAttribute("I am the color Blue")] 
    Blue, 
    [System.ComponentModel.DescriptionAttribute("I am the color Black")] 
    Black, 
    [System.ComponentModel.DescriptionAttribute("I am the color White")] 
    White 
} 

public static class Helper 
{ 
    public static string GetDescription(this Enum value) 
    { 
     System.Reflection.FieldInfo field = value.GetType().GetField(value.ToString()); 
     System.ComponentModel.DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(System.ComponentModel.DescriptionAttribute)) as System.ComponentModel.DescriptionAttribute; 
     return attribute == null ? value.ToString() : attribute.Description; 
    } 
} 

爲什麼在ConvertFromTypeName如果基礎類型是Fruit或Color我無法調用GetDescription擴展方法?似乎無法從var中推斷出這一點。 我希望它像PrintEnums,GetDescription擴展一樣工作。枚舉類型並調用擴展方法

Printing Fruit 
Value: 8, Description: I am an apple 
Value: 9, Description: I am an Banana 
Value: 10, Description: I am an Grape 
Value: 11, Description: I am an Kiwi 
Value: 12, Description: I am an Orange 
Printing Color 
Value: 56, Description: I am the color Red 
Value: 57, Description: I am the color Green 
Value: 58, Description: I am the color Blue 
Value: 59, Description: I am the color Black 
Value: 60, Description: I am the color White 
Could not load TypeTester.NonExist 
TypeTester.Thing is not an enum. 
UnderlingType: TypeTester.Fruit 
Value: 8, Description: Apple 
Value: 9, Description: Banana 
Value: 10, Description: Grape 
Value: 11, Description: Kiwi 
Value: 12, Description: Orange 
UnderlingType: TypeTester.Color 
Value: 56, Description: Red 
Value: 57, Description: Green 
Value: 58, Description: Blue 
Value: 59, Description: Black 
Value: 60, Description: White 
+0

請參閱有關的常見問題解答在問題中使用簽名。 – 2012-01-13 02:48:05

回答

1

明白了。在ConvertFromTypeName方法的foreach我改變

Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.ToString()); 

Console.WriteLine("Value: {0}, Description: {1}", (int)thing, ((Enum)thing).GetDescription()); 

貌似所有我需要做的是投事情枚舉得到@擴展方法

0

您錯誤地將枚舉類型與其基礎類型混淆。看看這個代碼:

Type type = Type.GetType(typeName, true); 
if (!type.IsEnum) 
    throw new ArgumentException(typeName + " is not an enum."); 

Console.WriteLine("UnderlingType: {0}", type.UnderlyingSystemType); 
string description = string.Empty; 
foreach (var thing in Enum.GetValues((type.UnderlyingSystemType))) 
{ 
    ... 
} 

這裏,type是枚舉類型 - 所以你幹嗎打的基礎類型?在這兩種情況下,這都是int不是 enum類型,所以Enum.GetValues將明顯失敗。

你只是想:

foreach (var thing in Enum.GetValues(type)) 
+0

如果我取消註釋//Console.WriteLine("Value:{0},Description:{1}「,(int)thing,thing.GetDescription()),我仍然會收到錯誤消息。我得到錯誤'對象'不包含'GetDescription'的定義,並且最好的擴展方法重載'TypeTester.Helper.GetDescription(System.Enum)'有一些無效參數 – Eric 2012-01-13 02:38:28