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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:41:57+00:00 2026-05-16T05:41:57+00:00

Sorry if this is a stupid noob question please be gentle with me I’m

  • 0

Sorry if this is a stupid noob question please be gentle with me I’m trying to learn…

I want to test against the attribute methods of things like models and controllers. Mostly to make sure they have the right attrbute ie Required. But i’m also using this as an experiment with extension methods and Lambdas.

What I’d like is a method that when implimented looks some thing like

Controller controller = new Controller();
controller.MethodName(params).HasAttribute<AttributeName>();

Iveused extension methods a little but not to this degree.. I’m sure this should be simple enough to do but cant seem to get my generics etc correct.

  • 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-16T05:41:58+00:00Added an answer on May 16, 2026 at 5:41 am

    Perhaps you are looking for this:

    Controller controller = new Controller();
    bool ok = controller.GetMethod(c => c.MethodName(null, null))
        .HasAttribute<AttributeName>();
    

    What’s nice about writing it like this is that you have fully compile time support. All other solutions thus far use string literals to define the methods.

    Here are the implementations of the GetMethod and HasAttribute<T> extension methods:

    public static MethodInfo GetMethod<T>(this T instance,
        Expression<Func<T, object>> methodSelector)
    {
        // Note: this is a bit simplistic implementation. It will
        // not work for all expressions.
        return ((MethodCallExpression)methodSelector.Body).Method;
    }
    
    public static MethodInfo GetMethod<T>(this T instance,
        Expression<Action<T>> methodSelector)
    {
        return ((MethodCallExpression)methodSelector.Body).Method;
    }
    
    public static bool HasAttribute<TAttribute>(
        this MemberInfo member) 
        where TAttribute : Attribute
    {
        return GetAttributes<TAttribute>(member).Length > 0;
    }
    
    public static TAttribute[] GetAttributes<TAttribute>(
        this MemberInfo member) 
        where TAttribute : Attribute
    {
        var attributes = 
            member.GetCustomAttributes(typeof(TAttribute), true);
    
        return (TAttribute[])attributes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this might be a basic/stupid/noob question - I am just trying to tweak
Sorry if this is a stupid question, but I'm trying to learn ExtJS ;)
Sorry if this is a stupid question. I want to create an HTTP handler
Sorry for maybe this is stupid question, I just want download source code form
Sorry if this is a stupid noob question. I'm doing a very small project
Sorry if this is a stupid question, I have a javascript function like: Test.prototype.createElement
I'm used to MSSQL, not Mysql, so sorry for this probably stupid question. I'm
Sorry if this question seems stupid, but it's stumped me for a couple days,
Sorry if this is a stupid question, but is there an easy way to
Sorry if this is a stupid question... I've developed an application that creates absolute

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.