A Lightweight Class Library for Generating Dynamic Proxy Objects Based on Emit.
public class Calculate : ICalculate {
public virtual int Add(int a, int b) {
return a + b;
}
}
public interface ICalculate {
int Add(int a, int b);
}
public class LogInterceptor : IInterceptor {
public void Intercept(IInvocation invocation) {
Debug.WriteLine("LogInterceptor pre....");
invocation.Proceed();
Debug.WriteLine("LogInterceptor post....");
}
}
var builder = new DefaultProxyBuilder(new ModuleScope());
var proxyGenerator = new DefaultProxyGenerator(builder);
//Using a public parameterless constructor
var calculator = proxyGenerator.CreateInterfaceProxy<ICalculate,Calculate>(new LogInterceptor());
//or Specify Constructor
calculator = proxyGenerator.CreateInterfaceProxy<ICalculate,Calculate>(new object[]{"test"},new LogInterceptor());
//or with target
calculator = proxyGenerator.CreateInterfaceProxyWithTarget<ICalculate,Calculate>(new Calculate(),new LogInterceptor());
calculator.Add(1, 3);
//Ignore a method's proxy
//Add the Ignore attribute on the method
[Ignore]
public int Multiply(int a, int b) {
return a * b;
}
TODO
- Dependency Injection
- Code optimization
PS: This project draws on some of the code design from castle. dynamic. We hope everyone can point out more issues that need improvement, learn together, and improve together.