Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7041463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:02:56+00:00 2026-05-28T02:02:56+00:00

I have an example class public class MyClass{ ActionResult Method1(){ …. } [Authorize] ActionResult

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:02:57+00:00Added an answer on May 28, 2026 at 2:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have a simple class like public class Person{ public int Age
I have the following example-class: public class MyClass<T> { public IList<T> GetAll() { return
I have a static method on a public class. Example: public class MyClass {
I have an example class containing two data points: public enum Sort { First,
Let's have a following example: public class X { } public class Y {
Example-I have a person class Public Class Person Private _fname As String Public Property
I have a custom c# type like (just an example): public class MyVector {
Say suppose I have the following Java code. public class Example { public static
Do I have to lock access to instance members? Example: public class HttpModule :
Example I have a repository class (DAL): public class MyRepository : IMyRepository { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.