What's the best way to implement a dynamic proxy in C#?

Posted by gap on Stack Overflow See other posts from Stack Overflow or by gap
Published on 2011-01-04T19:44:39Z Indexed on 2011/01/04 19:53 UTC
Read the original article Hit count: 163

Hi,

I've got a need to create a dynamic proxy in C#. I want this class to wrap another class, and take on it's public interface, forwarding calls for those functions:

class MyRootClass
{
    public virtual void Foo()
    {
        Console.Out.WriteLine("Foo!");
    }

}

interface ISecondaryInterface
{
    void Bar();
}

class Wrapper<T> : ISecondaryInterface where T: MyRootClass
{
    public Wrapper(T otherObj)
    {
    }

    public void Bar()
    {
        Console.Out.WriteLine("Bar!");
    }
}

Here's how I want to use it:

Wrapper<MyRootClass> wrappedObj = new Wrapper<MyRootClass>(new MyRootClass());
wrappedObj.Bar();
wrappedObj.Foo();

to produce:

Bar!
Foo!

Any ideas?

What's the easiest way to do this?

What's the best way to do this?

Thanks so much.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET