.net - Extension methods and Enums -


i have enum

public enum myenum     <stringvalue("bla bla")> _     blabla      <stringvalue("bbble bbble")> _     bleble end enum 

i did extension method (getstringvalue) takes enum , returns value of stringvalueattribute, if any.

i can dim svalue = myenum.blabla.getstringvalue()

now, want "extension" method returns me enum values list of strings.

i want apply this: myenum.getstringvalues()

is possible?

while can't add static extension method type, declare new class follows:

class enumhelper {     public static ienumerable<string> getstringvalues<tenum>()         tenum : struct, icomparable, iformattable, iconvertible     {         var enumtype = typeof(tenum);          if (!enumtype.isenum) {             throw new argumentexception("t must enumerated type");         }          foreach (enum item in enum.getvalues(enumtype))         {             yield return item.getstringvalue();         }     } } 

and call 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);     } } 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -