Justa.DynamicProxy

Overview

Justa.DynamicProxy is a simple API that helps you create dynamic proxies on the fly. Through the ProxyFactory class you can specify an Interface, target implementation of the interface, method invoker and presto you have a dynamic proxy.

Justa.DynamicProxy does not use Reflection.Emit or any byte code manipulation. The framework is based on a common trick used by many to generate dynamic proxies. The .NET Remoting library includes a quick and dirty way to generate dynamic proxies. Using .NET Remoting for dynamic proxy generation has one drawback. Generated proxies go through the Remoting stack for method invocations. Other dynamic proxy generators based on Reflection.Emit might perform faster, but for those of you who are simply interested in a quick and dirty way to generate dynamic proxies Just.DynamicProxy is for you.

Example

Example: creates a dynamic proxy around the specified CarService and delegates execution to the Invoker method.
  using Justa.Reflection;
  
  public interface ICarService 
  {
    void StartCar();
  }
  
  public class CarService : ICarService
  {
    public void StartCar() 
    {
      // start car...
    }
  }
  
  public class Runner
  {  
    public static void Main(string[] args) 
    {
      ICarService proxy = ProxyFactory<ICarService>(new CarService(), Invoker);
      proxy.Calculate(10, 5);
    }

    public static object Invoker(object target, MethodInfo method, object[] parameters) 
    {
       // do something before.

       object result = method.Invoke(target, parameters);

      // do something after.

      return result;
     }
  }


Last edited Jun 6, 2007 at 11:17 PM by aperez, version 14

Comments

No comments yet.