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

The Archive Base Latest Questions

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

OK, I’ve tried but I just don’t get it. I have two classes logger

  • 0

OK, I’ve tried but I just don’t get it.

I have two classes logger and class1.

I have a method called logger.Write(string) and method called class1.Execute().

Now in my application I want to have logger.Write(class1.ToString()) run when class1.Execute() is called.

I presume you need to add delegates and events, but I just can’t get my head around this, been scratching it for a couple hours.

One thing, is that the logger and the class are in different namespaces and I don’t want to update the class code for either of them if possible.

  • 1 1 Answer
  • 1 View
  • 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-11T20:59:10+00:00Added an answer on May 11, 2026 at 8:59 pm

    Well you certainly can’t do it without changing code in either class (assuming you also don’t want to change everywhere that calls class1.Execute) – at least not without some deep code-weaving/instrumentation magic. However, you can fairly easily add an event in Class1:

    public class Class1
    {
        // TODO: Think of a better name :)
        public event EventHandler ExecuteCalled = delegate {};
    
        public void Execute()
        {
            ExecuteCalled(this, EventArgs.Empty);
            // Do your normal stuff
        }
    }
    

    The delegate{} bit is just to make sure that there’s always at least a no-op event handler registered – it means you don’t need to check for nullity.

    You’d then hook it up by writing:

    Class1 class1 = new Class1();
    Logger logger = new Logger();
    class1.ExecuteCalled += (sender, args) => logger.Write(sender.ToString());
    

    (This is assuming you’re using C# 3 so you have lambda expressions available to you – let me know if that’s not the case.)

    If Class1 implements an interface (say IFoo), you might want to write an implementation of the interface which wraps another implementation, and just logs before each call:

    public sealed class LoggingFoo : IFoo
    {
        private readonly IFoo original;
        private readonly IFoo logger;
    
        public LoggingFoo(IFoo original, Logger logger)
        {
            // TODO: Check arguments for nullity
            this.original = original;
            this.logger = logger;
        }
    
        // Implement IFoo
        public void Execute()
        {
            logger.Write("Calling Execute on {0}", original);
            original.Execute();
        }
    }
    

    Then just use that wrapper around a “real” implementation wherever you currently just use the implementation.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.