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?Кажется, не в состоянии сделать вывод, что из вар.Я хотел бы, чтобы это работало так же, как 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