0

I have a Enum

Public Enum MyEnum
    <StringValue("Bla Bla")> _
    BlaBla

    <StringValue("bbble bbble")> _
    BleBle
End Enum

I did an extension method (GetStringValue) that takes an Enum and returns the value of the StringValueAttribute, if any.

I can do Dim sValue = MyEnum.BlaBla.GetStringValue()

Now, I want an "extension" method that returns to me all the Enum Values as a list of strings.

I want to apply it like this: MyEnum.GetStringValues()

Is it possible?

serhio
  • 28,010
  • 62
  • 221
  • 374

2 Answers2

3

While you can't add a static extension method to a type, you could declare a new class as follows:

class EnumHelper
{
    public static IEnumerable<string> GetStringValues<TEnum>()
        where TEnum : struct, IComparable, IFormattable, IConvertible
    {
        var enumType = typeof(TEnum);

        if (!enumType.IsEnum) {
            throw new ArgumentException("T must be an enumerated type");
        }

        foreach (Enum item in Enum.GetValues(enumType))
        {
            yield return item.GetStringValue();
        }
    }
}

and call it as follows:

class Program
{
    enum Rainbow { Red, Orange, Yellow, Green, Blue, Indigo, Violet }

    static void Main(string[] args)
    {
        foreach (var item in EnumHelper.GetStringValues<Rainbow>())
        {
            Console.WriteLine(item);

        }

        Console.ReadKey(false);
    }
}
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • can you add Enum constraints to the generic method type parameter? – serhio Jul 01 '11 at 14:06
  • Unfortunately I don't think you can, the best you could do is either wait for Jon Skeet to answer (I think he covers it in his book) or use reflection on the type to check if it's an enum and throw an exception is it's not. – Patrick McDonald Jul 01 '11 at 14:16
  • @serhio: No, the best you can do is value type constraint. – jason Jul 01 '11 at 14:16
  • actually, see http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum – Patrick McDonald Jul 01 '11 at 14:17
  • @Patrick: You can actually constrain slightly tighter using `where TEnum : struct, IComparable, IConvertible, IFormattable`. (And +1, I would've given pretty much the same answer.) – LukeH Jul 01 '11 at 14:34
2

Is it possible?

No. You can add extension methods to instances of specific types, but not to specific types themselves.

Here's one way to think about this: extension methods are merely syntactic sugar for static methods on static classes where first parameter of the method is the "receiver" of the extension method. Is it possible to write a static method on a static class that has its first parameter something like MyEnum? No. You can't pass type names as parameters to methods (you can passes instances of type handles (e.g., typeof(MyEnum)), but not the type name itself). Thus, what you are asking is not possible.

jason
  • 236,483
  • 35
  • 423
  • 525
  • wondering where do you reccomend to add this method? an extention method to a instance of tahat enum? – serhio Jul 01 '11 at 13:48