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 9131105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:05:18+00:00 2026-06-17T08:05:18+00:00

I want to write a method that will analyze custom attributes of any method

  • 0

I want to write a method that will analyze custom attributes of any method (with any number of arguments and any return type) knowing only method info.
This function will check if method has specific Attribute. like this: var tmp = methodInfo.GetCustomAttributes(typeof(LineItemAttribute),false); and if it has such attribute It will execute it.And I want to make call of that function really easy to use. So, in example there are three methods and method GetMethodAttributes that I want to call.

class Test
{
      public static void Main()
      {
      }

      public void Test1(){}

      public void Test2(int a){}

      public void Test3(object a, string c, Boolean d);

      public void GetMethodAttributes(MethodInfo mi) {}
}

Ideally I want to write something like that

public static void Main()
    {
        var t = new Test();
        GetMethodAttributes(t.Test1);
        GetMethodAttributes(t.Test2);
        GetMethodAttributes(t.Test3);
    }

I don’t want to use string representation of the method names as method names may change, like that:

MethodInfo info = type.GetMethod(name);

Do I have any options? Basically I need a way to use delegates for functions with different sinatures

  • 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-06-17T08:05:19+00:00Added an answer on June 17, 2026 at 8:05 am

    As Chris Sinclair pointed out in the comment above; you can use a delegate without using reflection or expression trees to get the MethodInfo. The downside is that the compiler is not able to infer the generic parameter so you have to specify the delegate type to match the signature of the given method like this:

    public class Test
    {
        public static void Main()
        {
            var t = new Test();
            CheckMethodAttributes<Action>(t.Test1);
            CheckMethodAttributes<Action<int>>(t.Test2);
            CheckMethodAttributes<Action<object, string, bool>>(t.Test3);
        }
    
        public void Test1() { }
    
        public void Test2(int a) { }
    
        public void Test3(object a, string c, bool d) { }
    
        public static void CheckMethodAttributes<T>(T func)
        {
            MethodInfo method = new MethodOf<T>(func);
    
            // Example attribute check:
            var ignoreAttribute = method.GetAttribute<IgnoreAttribute>();
            if (ignoreAttribute != null)
            {
                // Do something here...
            }
        }
    }
    

    This uses two utility classes, the MethodOf<T> for extracting the MethodInfo from the given Delegate and some AttributeUtils to get strongly typed custom attribute retrieval:

    public static class AttributeUtils
    {
        public static bool HasAttribute<TAttribute>(this MemberInfo member, bool inherit = true)
            where TAttribute : Attribute
        {
            return member.IsDefined(typeof(TAttribute), inherit);
        }
    
        public static TAttribute GetAttribute<TAttribute>(this MemberInfo member, bool inherit = true)
            where TAttribute : Attribute
        {
            return member.GetAttributes<TAttribute>(inherit).FirstOrDefault();
        }
    
        public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this MemberInfo member, bool inherit = true)
            where TAttribute : Attribute
        {
            return member.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>();
        }
    }
    
    public class MethodOf<T>
    {
        public MethodOf(T func)
        {
            var del = func as Delegate;
            if (del == null) throw new ArgumentException("Cannot convert func to Delegate.", "func");
    
            Method = del.Method;
        }
    
        private MethodInfo Method { get; set; }
    
        public static implicit operator MethodOf<T>(T func)
        {
            return new MethodOf<T>(func);
        }
    
        public static implicit operator MethodInfo(MethodOf<T> methodOf)
        {
            return methodOf.Method;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a C# method that can accept any number. Something like:
I want to write a generic extension method that will return a specified set
I want to write a method that will take an integer and return a
I want to write a test case that will verify a method in my
I'm trying to write a method that will return a Hibernate object based on
I want to create a method that will execute a query and return a
I want to write a method that when supplied an array of ints will
I want to write a method that when supplied an array of ints will
I want to write a method that run multiple threads and I want before
I want to write a tester that will test a http server I wrote

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.