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;
    }
}

Why in ConvertFromTypeName if the underlying type is Fruit or Color I cannot call the GetDescription extension method? It seems to not to be able to infer that from the var. I would like this to work just like PrintEnums, the GetDescription extension.

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
1
  • Please see the FAQ regarding the use of signatures in questions. Commented Jan 13, 2012 at 2:48

2 Answers 2

1

Got it. In the foreach of ConvertFromTypeName method I changed

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

to

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

Looks like all I needed to do was cast thing to an Enum to get @ the extension method

Sign up to request clarification or add additional context in comments.

Comments

0

You're mistaking the enum type with its underlying type. Look at this code:

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)))
{
    ...
}

Here, type is the enum type - so why are you fetching the underlying type? That would be int in both the cases you've given. That isn't an enum type, so Enum.GetValues will clearly fail.

You just want:

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

1 Comment

I still get an error if I uncomment out //Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription()); I get Error 2 'object' does not contain a definition for 'GetDescription' and the best extension method overload 'TypeTester.Helper.GetDescription(System.Enum)' has some invalid arguments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.