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

  • Home
  • SEARCH
  • 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 431553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:59:10+00:00 2026-05-12T19:59:10+00:00

all. Maybe i’ve not googled enough, but i can’t find any example on this

  • 0

all. Maybe i’ve not googled enough, but i can’t find any example on this question.

It is possible in C# to create an custom attribute which, applied to a class, modifies all of its methods? For example, adds Console.WriteLine("Hello, i'm modified method"); as a first line (or it’s IL equivalent if it’s done at runtime)?

  • 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-12T19:59:11+00:00Added an answer on May 12, 2026 at 7:59 pm

    Yes, you can do this, but no, its not built in to C#. As Eric says, this technique is known as Aspect Oriented Programming.

    I’ve used PostSharp at work, and it is very effective. It works at compile time, and uses IL-weaving, as opposed to other AOP techniques.

    For example, the following attribute will do what you want:

    [Serializable]
    [MulticastAttributeUsage(MulticastTargets.Method | MulticastTargets.Class,
                             AllowMultiple = true,
                             TargetMemberAttributes = MulticastAttributes.Public | 
                                                      MulticastAttributes.NonAbstract | 
                                                      MulticastAttributes.Managed)]
    class MyAspect : OnMethodInvocationAspect
    {
        public override void OnInvocation(MethodInvocationEventArgs eventArgs)
        {
            Console.WriteLine("Hello, i'm modified method");
    
            base.OnInvocation(eventArgs);
        }
    }
    

    You simply apply MyAspect as an attribute on your class, and it will be applied to every method within it. You can control how the aspect is applied by modifying the TargetmemberAttributes property of the MulticastAttributeUsage property. In this example, I want to restrict it to apply only to public, non-abstract methods.

    There’s a lot more you can do so have a look (at AOP in general).

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

Sidebar

Related Questions

First of all I am not a designer. so this question is may be
This may be a simple fix - but I'm trying to sum together all
I'm drawing a blank this late at night after programming all day and maybe
This is probably a stupidly simple question, but I'm stuck. I had created a
I have question regarding Java Comparator. Or maybe you all have another Idea to
Maybe I'm going about this all wrong. I have a bunch of classes that
Maybe the problem can be solved by deleting all those functions, can't it? However,
In Maybe Normalizing Isn't Normal Jeff Atwood says, You're automatically measuring all the queries
All, I'd like to check Django out and maybe hack together some web pages
I would like to customize the background (and maybe the border too) of all

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.