How to check if method has an attribute
There is a easier solution availabe compared to the others above with the current .NET/C# version (4.6.1, C#6):
If you only have one one method with that name:
var method = typeof(TestClass).GetMethods()
.SingleOrDefault(x => x.Name == nameof(TestClass.TestMethod));
var attribute = method?.GetCustomAttributes(typeof(MethodAttribute), true)
.Single() as MethodAttribute;
Now to check if you have the attribute set on the method:
bool isDefined = attribute != null;
And if you want to access the properties of the attribute, you can do this easy as that:
var someInfo = attribute.SomeMethodInfo
If there are multiple methods with the same name, you can go on and use method.GetParameters()
and check for the parameters, instead of .GetMethods().Single...
If you know that your method has no parameters, this check is easy:
var method = typeof(TestClass).GetMethods()
.SingleOrDefault(
x => x.Name == nameof(TestClass.TestMethod)
&& x.GetParameters().Length == 0
);
If not, this is going to be more complicated (checking parameters, etc.) and the other solutions are way easier and robust to use.
So: Use this if you have no overloads for a method, or only want to read attributes from a method with a specified amount of parameters.
Else, use the MethodOf
provided by other answers in here.
The issue with your code is the signature of public bool MethodHasAuthorizeAttribute(Func<int, ActionResult> function)
. MethodHasAuthorizeAttribute
can only be used with arguments matching the signature of the delegate you specified. In this case a method returning an ActionResult
with a parameter of type int
.
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 call MethodHasAuthorizeAttribute(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:
public static MethodInfo MethodOf( Expression<System.Action> expression )
{
MethodCallExpression body = (MethodCallExpression)expression.Body;
return body.Method;
}
You can use it like this, but it can also be used with any method:
MethodInfo method = MethodOf( () => controller.Method3( default( int ) ) );
With that out of the way, we can build a general implementation:
public static bool MethodHasAuthorizeAttribute( Expression<System.Action> expression )
{
var method = MethodOf( expression );
const bool includeInherited = false;
return method.GetCustomAttributes( typeof( AuthorizeAttribute ), includeInherited ).Any();
}
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 for Type
, FieldInfo
and MethodInfo
. This as easy as extracting the Attribute search into a separate method and providing appropriate adapter methods with nice names:
public static bool MethodHasAuthorizeAttribute( Expression<System.Action> expression )
{
MemberInfo member = MethodOf( expression );
return MemberHasAuthorizeAttribute( member );
}
public static bool TypeHasAuthorizeAttribute( Type t)
{
return MemberHasAuthorizeAttribute( t );
}
private static bool MemberHasAuthorizeAttribute( MemberInfo member )
{
const bool includeInherited = false;
return member.GetCustomAttributes( typeof( AuthorizeAttribute ), includeInherited ).Any();
}
I'll leave the implementation for fields as an exercise, you can employ the same trick as MethodOf.