Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created August 28, 2012 13:56
Show Gist options
  • Save loosechainsaw/3498212 to your computer and use it in GitHub Desktop.
Save loosechainsaw/3498212 to your computer and use it in GitHub Desktop.
Dynamic Proxy
using System;
using System.Linq;
using System.Reflection;
using Castle.DynamicProxy;
namespace DpWorkshop
{
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class AuditingInterceptor : IInterceptor
{
public int Count { get; private set; }
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Invoking: {0}",invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("Invoked: {0}", invocation.Method.Name);
}
}
public class AuditingProxyGenerationHook : IProxyGenerationHook
{
public AuditingProxyGenerationHook()
{
}
public void MethodsInspected()
{
}
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
{
Console.WriteLine("Can not proxy method: {0}", memberInfo.Name);
}
public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
{
return (methodInfo.Name.StartsWith("set_") || methodInfo.Name.StartsWith("get_")) && methodInfo.IsSpecialName;
}
}
public class AuditingProxyInterceptorSelector : IInterceptorSelector
{
public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
{
if(method.Name.StartsWith("set_"))
return new IInterceptor[]{ new AuditingInterceptor()};
return null;
}
}
public class AuditingProxyFactory
{
public static T CreateClassProxy<T>() where T : class
{
var options = new ProxyGenerationOptions(new AuditingProxyGenerationHook())
{
Selector = new AuditingProxyInterceptorSelector()
};
var proxyGenerator = new ProxyGenerator();
return proxyGenerator.CreateClassProxy<T>(options, new AuditingInterceptor());
}
}
public interface IIMethodsIAmIntercepting
{
void Call(int a);
void Call();
}
public class MethodCallToJsonInvokerInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine(invocation.Method.Name);
}
}
public class JsonMethodProxyFactory
{
public static TInterface Create<TInterface>() where TInterface : class
{
var proxyGenerator = new ProxyGenerator();
return proxyGenerator.CreateInterfaceProxyWithoutTarget<TInterface>(new MethodCallToJsonInvokerInterceptor());
}
}
class Program
{
static void Main(string[] args)
{
//ClassProxyExample();
var a = JsonMethodProxyFactory.Create<IIMethodsIAmIntercepting>();
a.Call(1);
a.Call(1);
a.Call(1);
Console.WriteLine("Press any key to exit....");
Console.ReadKey();
}
private static void ClassProxyExample()
{
var person = AuditingProxyFactory.CreateClassProxy<Person>();
person.Id = 1;
person.Name = "Blair Davidson";
Console.WriteLine("Person: {0}, {1}", person.Id, person.Name);
CastToIProxyTargetAccessor(person);
}
private static void CastToIProxyTargetAccessor(Person person)
{
var target = person as IProxyTargetAccessor;
var count = target.GetInterceptors().OfType<AuditingInterceptor>().First().Count;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment