Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created May 5, 2012 21:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martinnormark/2605628 to your computer and use it in GitHub Desktop.
Save martinnormark/2605628 to your computer and use it in GitHub Desktop.
Method for verifying that a method contains an Attribute
/// <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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment