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 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

This may be a silly question, but I'm not all too experienced with iPhone
Maybe im missing something but im not sure. All im simply trying to do
Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all I am not a designer. so this question is may be
This could be a duplicate question, but I have no idea what search terms
This is probably a stupidly simple question, but I'm stuck. I had created a
I have a problem with preg_match_all that maybe you can help. I have a
Maybe seeing the issue would help you all a little: https://ws.onehub.com/files/nzvrbj4s Screen Shots: https://ws.onehub.com/files/9rw3li12
As you maybe know, as Java language perspective all method in C# are final
I have a grouped UITableView where not all sections may be displayed at once,

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.