Does C# allow method overloading, PHP style (__call)?

Posted by mr.b on Stack Overflow See other posts from Stack Overflow or by mr.b
Published on 2010-06-05T14:28:58Z Indexed on 2010/06/05 14:32 UTC
Read the original article Hit count: 219

In PHP, there is a special method named __call($calledMethodName, $arguments), which allows class to catch calls to non-existing methods, and do something about it.

Since most of classic languages are strongly typed, compiler won't allow calling a method that does not exist, I'm clear with that part.

What I want to accomplish (and I figured this is how I would do it in PHP, but C# is something else) is to proxy calls to a class methods and log each of these calls.

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();
pc.inner.B();
// ...

So, how can I proxy calls to an object instance in extensible way? 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!

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns