c# - How to get Custom Attribute values for enums? -


i have enum each member has custom attribute applied it. how can retrieve value stored in each attribute?

right this:

var attributes = typeof ( effecttype ).getcustomattributes ( false ); foreach ( object attribute in attributes ) {     gpushaderattribute attr = ( gpushaderattribute ) attribute;     if ( attr != null )         return attr.gpushader; } return 0; 

another issue is, if it's not found, should return? 0 implcity convertible enum, right? that's why returned that.

forgot mention, above code returns 0 every enum member.

it bit messy trying have use reflection:

public gpushaderattribute getgpushader(effecttype effecttype) {     memberinfo memberinfo = typeof(effecttype).getmember(effecttype.tostring())                                               .firstordefault();      if (memberinfo != null)     {         gpushaderattribute attribute = (gpushaderattribute)                       memberinfo.getcustomattributes(typeof(gpushaderattribute), false)                                .firstordefault();         return attribute;     }      return null; } 

this return instance of gpushaderattribute relevant 1 marked on enum value of effecttype. have call on specific value of effecttype enum:

gpushaderattribute attribute = getgpushader(effecttype.myeffect); 

once have instance of attribute, can specific values out of marked-up on individual enum values.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -