I have an example class
public class MyClass{
ActionResult Method1(){
....
}
[Authorize]
ActionResult Method2(){
....
}
[Authorize]
ActionResult Method3(int value){
....
}
}
Now what I want is to write a function returning true/false that can be executed like this
var controller = new MyClass();
Assert.IsFalse(MethodHasAuthorizeAttribute(controller.Method1));
Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method2));
Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method3));
I got to the point where
public bool MethodHasAuthorizeAttribute(Func<int, ActionResult> function)
{
return function.Method.GetCustomAttributes(typeof(AuthorizeAttribute), false).Length > 0;
}
would work for Method3. Now how can I do that generic in a way that it’ll take strings and classes as parameters as well?
The issue with your code is the signature of
public bool MethodHasAuthorizeAttribute(Func<int, ActionResult> function).MethodHasAuthorizeAttributecan only be used with arguments matching the signature of the delegate you specified. In this case a method returning anActionResultwith a parameter of typeint.When you call this method like
MethodHasAuthorizeAttribute(controller.Method3), the Compiler will do a method group conversion. This might not always be desired and can yield unexpected results (Method group conversions aren’t always straigthforward). If you try to callMethodHasAuthorizeAttribute(controller.Method1)you will get a compiler error because there’s no conversion.A more general solution can be constructed with expression trees and the famous “MethodOf” trick. It employs compiler generated expression trees to find the invocation target:
You can use it like this, but it can also be used with any method:
With that out of the way, we can build a general implementation:
Okay, thats for methods. Now, if you want to apply the Attribute check on classes or fields to (I’ll spare properties because they are actually methods), we need to perform our check on
MemberInfo, which is the inheritance root forType,FieldInfoandMethodInfo. This as easy as extracting the Attribute search into a separate method and providing appropriate adapter methods with nice names:I’ll leave the implementation for fields as an exercise, you can employ the same trick as MethodOf.