c# - How to check if method has an attribute -



c# - How to check if method has an attribute -

i have illustration class

public class myclass{ actionresult method1(){ .... } [authorize] actionresult method2(){ .... } [authorize] actionresult method3(int value){ .... } }

now want write function returning true/false can executed this

var controller = new myclass(); assert.isfalse(methodhasauthorizeattribute(controller.method1)); assert.istrue(methodhasauthorizeattribute(controller.method2)); assert.istrue(methodhasauthorizeattribute(controller.method3));

i got point where

public bool methodhasauthorizeattribute(func<int, actionresult> function) { homecoming function.method.getcustomattributes(typeof(authorizeattribute), false).length > 0; }

would work method3. how can generic in way it'll take strings , classes parameters ?

the issue code signature of public bool methodhasauthorizeattribute(func<int, actionresult> function). methodhasauthorizeattribute can used arguments matching signature of delegate specified. in case method returning actionresult parameter of type int.

when phone call method methodhasauthorizeattribute(controller.method3), compiler method grouping conversion. might not desired , can yield unexpected results (method grouping conversions aren't straigthforward). if seek phone call methodhasauthorizeattribute(controller.method1) compiler error because there's no conversion.

a more general solution can constructed look trees , famous "methodof" trick. employs compiler generated look trees find invocation target:

public static methodinfo methodof( expression<system.action> look ) { methodcallexpression body = (methodcallexpression)expression.body; homecoming body.method; }

you can utilize this, can used method:

methodinfo method = methodof( () => controller.method3( default( int ) ) );

with out of way, can build general implementation:

public static bool methodhasauthorizeattribute( expression<system.action> look ) { var method = methodof( look ); const bool includeinherited = false; homecoming method.getcustomattributes( typeof( authorizeattribute ), includeinherited ).any(); }

okay, thats methods. now, if want apply attribute check on classes or fields (i'll spare properties because methods), need perform our check on memberinfo, inheritance root type, fieldinfo , methodinfo. easy extracting attribute search separate method , providing appropriate adapter methods nice names:

public static bool methodhasauthorizeattribute( expression<system.action> look ) { memberinfo fellow member = methodof( look ); homecoming memberhasauthorizeattribute( fellow member ); } public static bool typehasauthorizeattribute( type t) { homecoming memberhasauthorizeattribute( t ); } private static bool memberhasauthorizeattribute( memberinfo fellow member ) { const bool includeinherited = false; homecoming member.getcustomattributes( typeof( authorizeattribute ), includeinherited ).any(); }

i'll leave implementation fields exercise, can employ same trick methodof.

c# reflection attributes tdd assert

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -