Forked from martinnormark/MvcActionFilterTestHelper.cs
Last active
August 29, 2015 14:11
-
-
Save javafun/28681038ce20a609c516 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Verifies the controller action, contains an attribute of the specified attributeType. | |
/// </summary> | |
/// <param name="controller">The controller.</param> | |
/// <param name="action">The action method.</param> | |
/// <param name="attributeType">Type of the attribute to look for.</param> | |
/// <returns>Returns true if the attribute was present on the action. Otherwise false.</returns> | |
public static bool VerifyControllerActionAttribute(this Controller controller, Func<ActionResult> action, Type attributeType) | |
{ | |
MethodInfo methodInfo = action.Method; | |
object[] attributes = methodInfo.GetCustomAttributes(attributeType, true); | |
return attributes.Any(a => a.GetType() == attributeType); | |
} | |
public static bool VerifyActionAttribute<TAttribute, T>(Expression<Func<T>> expression) | |
{ | |
var methodCallExp = (expression.Body as MethodCallExpression); | |
if (methodCallExp != null) | |
{ | |
var action = methodCallExp.Method; | |
var attributes = action.GetCustomAttributes(typeof (TAttribute),false); | |
return attributes.Length > 0; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment