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.
I should have written this sooner, but never mind.
My issue had a special “gotcha” I needed to be able to proxy classes and not interfaces.
There are two solutions to this:
RealProxy and friends, basically means using .NET Remoting. Requires one to inherit from ContextBoundObject.
(which is hardcoded to specifically recognize
RealProxy) to let you “override” non-virtual members.Building a proxy using System.Reflection.Emit as done by spring you can also look at the code of their ProxyFactoryObject. Here are another three articles on the subject.
virtualmembers.