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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:11:03+00:00 2026-05-15T04:11:03+00:00

Edit: Changed question title from Does C# allow method overloading, PHP style (__call)? –

  • 0

Edit: Changed question title from “Does C# allow method overloading, PHP style (__call)?” – figured out it doesn’t have much to do with actual question. Also edited question text.

What I want to accomplish is to proxy calls to a an instance of an object methods, so I could log calls to any of its methods.

Right now, I have code similar to this:

class ProxyClass {
    static logger;
    public AnotherClass inner { get; private set; }
    public ProxyClass() { inner = new AnotherClass(); }
}

class AnotherClass {
    public void A() {}
    public void B() {}
    public void C() {}
    // ...
}

// meanwhile, in happyCodeLandia...
ProxyClass pc = new ProxyClass();
pc.inner.A(); // need to write log message like "method A called"
pc.inner.B(); // need to write log message like "method B called"
// ...

So, how can I proxy calls to an object instance in extensible way? Method overloading would be most obvious solution (if it was supported in PHP way). By extensible, meaning that I don’t have to modify ProxyClass whenever AnotherClass changes.

In my case, AnotherClass can have any number of methods, so it wouldn’t be appropriate to overload or wrap all methods to add logging.

I am aware that this might not be the best approach for this kind of problem, so if anyone has idea what approach to use, shoot.

Thanks!

  • 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-15T04:11:03+00:00Added an answer on May 15, 2026 at 4:11 am

    Echoing the two other; DI is the way to go. Dynamic Proxy is very competent in this respect.

    Here is some example code, complete with implementation of all that is required. Normally it’s good practice to code against an interface.

    I can recommend reading a bit about AOP, here’s my thread on it:
    Help and Information about Aspect Oriented Programming

    (Edit: Becuase you were nice and gave me some points, here’s another cool link to a DP tutorial that’s really well done: http://kozmic.pl/archive/2009/04/27/castle-dynamic-proxy-tutorial.aspx ;))

    Here is example code:

    using System;
    using System.Collections.Generic;
    using Castle.Core;
    using Castle.Core.Interceptor;
    using Castle.MicroKernel.Registration;
    using Castle.Windsor;
    using NUnit.Framework;
    
    [TestFixture]
    public class LoggingMethodInvocationsTests
    {
        [Test]
        public void CanLogInvocations()
        {
            var container = new WindsorContainer();
            container.Register(Component.For<LoggingInterceptor>().LifeStyle.Singleton);
            // log all calls to the interface
            container.Register(Component.For<IA>().ImplementedBy<A>().Interceptors(typeof (LoggingInterceptor)));
    
            var a = container.Resolve<IA>();
            a.AMethod(3.1415926535); // to interface
            Console.WriteLine("End of test");
        }
    }
    
    public class LoggingInterceptor : IInterceptor, IOnBehalfAware
    {
        private string _entityName;
    
        public void Intercept(IInvocation invocation)
        {
            var largs = new List<string>(invocation.Arguments.Length);
    
            for (int i = 0; i < invocation.Arguments.Length; i++)
                largs.Add(invocation.Arguments[i].ToString());
    
            var a = largs.Count == 0 ? "[no arguments]" : string.Join(", ", largs.ToArray());
            var method = invocation.Method == null ? "[on interface target]" : invocation.Method.Name;
    
            Console.WriteLine(string.Format("{0}.{1} called with arguments {2}", _entityName, method, a));
    
            invocation.Proceed();
    
            Console.WriteLine(string.Format("After invocation. Return value {0}", invocation.ReturnValue));
        }
    
        public void SetInterceptedComponentModel(ComponentModel target)
        {
            if (target != null)
                _entityName = target.Implementation.FullName;
        }
    }
    
    public class A : IA
    {
        public double AMethod(double a)
        {
            Console.WriteLine("A method impl");
            return a*2;
        }
    
        public void SecondMethod(double a)
        {
            Console.WriteLine(string.Format("Impl: SecondMethod called with {0}", a));
        }
    }
    
    public interface IA
    {
        double AMethod(double a);
    }
    

    Console output

    Examples.A.AMethod called with arguments 3,1415926535
    A method impl
    After invocation. Return value 6,283185307
    End of test
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Rails has support for RESTful web-services OOTB, so if you… May 16, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer I don't know what exactly you are trying to match,… May 16, 2026 at 5:07 pm
  • Editorial Team
    Editorial Team added an answer You swf is throwing a security error, could be causing… May 16, 2026 at 5:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Edit: My original title has been sort of changed. I suspect the current title
EDIT: changed the title to fit the code below. I'm trying to retrieve a
Does anyone have a great system, or any ideas, for doing as the title
Apologies for the title, I found it hard to define my question succintly and
I am not sure if the title formulates it well so sorry. I basically
The title explains all... I have this snippet of code in my application: String
Trying to migrate from MSSQL to MySQL. This Stored Proc is creating a temp
I have a blogpost edit page where you can either save your edits or
1) does anyone know something about the adoption rate of WSDL 2.0? I always
I've got an XML file which I use to create objects, change the objects,

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.