How to make a simple dynamic proxy in C #

I want to create a dynamic proxy object to add specific functions to the object.

basically I want to get an object, wrap it with an object that looks identical to the original that I received, and intercept all 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 } } 



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




I add generosity because I need a good way for proxy classes (not interfaces) and for handling non-virtual methods (as if I had inherited and added metond under the keyword “new”). I am sure that all this is very possible, since .Net does this.

+47
reflection c # aop proxy
Dec 05 2018-11-12T00:
source share
6 answers

I should have written this before, but never mind.

My problem had a special "gotcha", I needed to have proxy classes, not interfaces.

There are two solutions:

By the way, the second approach has significant limitations; you cannot proxy non-virtual methods.

+12
Jan 12 '12 at 8:40
source share

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

 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 can refuse the choice of type of safety and go with DynamicObject, as I showed, and then refuse duck skating.

I made a transparent extensible version of this object proxy and opened it here .

+33
Dec 05 2018-11-12T00:
source share

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

+12
Dec 23 '11 at 14:57
source share

Take a look at PostSharp . I don’t know how to do what you want in vanilla.Net, but PostSharp offers things like “OnMethodBoundaryAspect”, which can be used to replace or port code inside a method.

I used it to do things like logging, checking parameters, handling exceptions, etc.

There is a free Community Edition that should work for you. You will need to install it on your development machine, as well as any build server that you use.

+5
Dec 05 '11 at 2:47 a.m.
source share

Here is a simple example Creating a dynamic proxy using C # Emit

You can also use an AOP structure like PostSharp

+2
Dec 05 '11 at 2:47 a.m.
source share

Another option is ContextBoundObject .

An article about 8-9 years ago appeared in CodeProject, using this approach to track method calls.

+2
Jan 11 '12 at 9:21
source share



All Articles