c# - How to get complex enum value string representation -


let's have enum:

[flags] public enum sometype {         val1 = 0,     val2 = 1,     val3 = 2,     val4 = 4,     val5 = 8,     val6 = 16,     = val1 | val2 | val3 | val4 | val5 | val6 } 

and variables:

sometype easytype = sometype.val1 | sometype.val2; sometype complextype = sometype.all; 

if want loop through values of first enum can do:

foreach(string s in easytype.tostring().split(',')) { ... } 

however, when try apply same approach 'complextype' value 'all', of course valid because it's 1 of possible values of enum. but, there neat way see of values sometype.all created of? know make manual loop through values that:

if(complextype.hasflag(manualtype.val1) && ... 

var result = string.join(",",                  enum.getvalues(typeof(sometype))                      .cast<sometype>()                      .where(v => complextype.hasflag(v))); 

you can write extension method avoid repeating yourself.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -