c# - Get attribute of property knowing what it is -


want simple , getting attribute of property. know how via propertyinfo etc know property want attribute of possible like:

myattribute attr = (myattribute)customer.forename; 

ideally want avoid reflection. way can think of doing linq statement like:

propertyinfo pi = typeof(customer).getproperties().where(x => x.name == "forename").firstordefault(); myattribute attri = (myattribute)attribute.getcustomattribute(pi, typeof(myattribute)); 

don't having string comparison on property name :(

considering know property want attribute of thought there might easier way?

kzu wrote piece of code allow typed reflection.

see blog post here: http://blogs.clariusconsulting.net/kzu/linq-beyond-queries-strong-typed-reflection/

you can nuget package (netfx reflector):

http://nuget.org/packages/netfx-reflector

// void static method methodinfo cw = reflect.getmethod(() => console.writeline);  // instance void method methodinfo mi = reflect<iview>.getmethod(v => v.show);  // boolean returning instance method methodinfo pi = reflect<iviewmodel>.getmethod<bool>(v => v.save); 

Comments