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 :(得分:1)
知道了。在ConvertFrom TypeName方法的foreach中,我改变了
Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.ToString());
到
Console.WriteLine("Value: {0}, Description: {1}", (int)thing, ((Enum)thing).GetDescription());
看起来我需要做的就是向Enum投射东西以获得@扩展方法
答案 1 :(得分: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.GetValues
显然会失败。
你只想:
foreach (var thing in Enum.GetValues(type))