Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a simple dynamic proxy in C#

I want to build a dynamic proxy object to add certain functionality to an object.

basically i want to receive an object, wrap it with an object that looks identical to the original i got, and intercept all the calls.

class Wrapper : DynamicProxy// dynamic proxy is not a reall class, but i guess something like this exists... {     public static T Wrap(T obj)     {         return (T) new Wrapper(obj);     }      public override object InterceptCall(MethodInfo info, object[] args)     {         // do stuff     }  } 

Just to clarify, I want to do something similar to the WCF channel factory...


I'm adding a bounty, because I need a good way to proxy classes (not interfaces) and to handle non virtual methods (as if I inherited and added a methond under the "new" keyword). I'm sure all this is very possible as the .Net does it.

like image 900
AK_ Avatar asked Dec 05 '11 14:12

AK_


People also ask

How do you create a dynamic proxy in Java?

Building a Dynamic Proxy. Creating a dynamic proxy involves creating a class that traps incoming method invocations. There is no built in C# mechanism to do this, so this must be done dynamically at runtime. In order to accomplish this, we first define an interface IProxyInvocationHandler.

What is proxy design pattern in C?

We can also say that the Proxy is the object which is being called by the client to access the real object behind the scene. That means, In Proxy Design Pattern, a class represents the functionality of another class. Please have a look at the following diagram for a better understanding of the Proxy Design Pattern in C#.

What are proxies in JavaScript?

As you can see in the following image, when the client wants to consume some methods of the Real Object, then he/she needs to go via the Proxy object. That means the client will call the method of the Proxy object and the proxy will be responsible to call the method of the Real Object. There are three types of proxies. They are as follows.

How to run simple proxy server in C using multi-threading?

Simple Proxy Server in C using Multi-threading 1 Compile and run main server 2 Compile and run proxy server 3 Compile and run client More ...


2 Answers

You could do this with a combination of DynamicObject and ImpromptuInterface but you will have to have an Interface that implements the functions and properties you want to proxy.

public interface IDoStuff {     void Foo(); }  public class Wrapper<T> : DynamicObject {     private readonly T _wrappedObject;      public static T1 Wrap<T1>(T obj) where T1 : class     {         if (!typeof(T1).IsInterface)             throw new ArgumentException("T1 must be an Interface");          return new Wrapper<T>(obj).ActLike<T1>();     }      //you can make the contructor private so you are forced to use the Wrap method.     private Wrapper(T obj)     {         _wrappedObject = obj;     }      public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)     {         try         {             //do stuff here              //call _wrappedObject object             result = _wrappedObject.GetType().GetMethod(binder.Name).Invoke(_wrappedObject, args);             return true;         }         catch         {             result = null;             return false;         }     } } 

You could of course choose to lose the type-safety and go with a DynamicObject like I showed and then drop the duck-casting.

I made a transparant extendible version of this object proxy, and open-sourced it here.

like image 95
albertjan Avatar answered Sep 18 '22 02:09

albertjan


In addition to Castle.DynamicProxy, there is also LinFu.DynamicProxy on Github.

like image 39
Harry Steinhilber Avatar answered Sep 18 '22 02:09

Harry Steinhilber