I need to create a dynamic proxy in C #. I want this class to wrap another class and take over the public interface by forwarding calls to these 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!"); } }
This is how I want to use it:
Wrapper<MyRootClass> wrappedObj = new Wrapper<MyRootClass>(new MyRootClass()); wrappedObj.Bar(); wrappedObj.Foo();
for creating:
Bar! Foo!
Any ideas?
What is the easiest way to do this?
What is the best way to do this?
Thank you very much.
UPDATE
I tried following the Wernight recommendation and implementing this with C # 4.0 dynamic proxies. Sorry, I'm still stuck. The proxy point should mimic another interface, which (usually usually) is expected. Using DynamicObject requires me to change all clients of this to use "dynamic" instead of "ISecondaryInterface".
Is there a way to get a proxy object, for example, when it wraps A, it advertises (statically?) That it supports interface A; and when he wraps B, he advertises that supports interface B?
UPDATE 2
For instance:
class MySecretProxy : DynamicObject, ISecondaryInterface { public override void TryInvokeMember(...) { .. }
source share