public static IRepository <T> Create() { var decorated = new Repository <T>(); var proxy = DynamicProxy <IRepository <T> > .Create(decorated, new LoggerProxyActions()); return(proxy); }
/// <summary> /// 获取指定类型的动态代理 /// </summary> /// <param name="genericType"></param> /// <param name="serviceType"></param> /// <returns></returns> private object GetProxyService(Type genericType, Type serviceType) { object result = null; Type proxyType = null; object proxy = null; if (genericType == typeof(IInvocation <>)) { proxyType = serviceType.GetGenericArguments()[0]; proxy = DynamicProxy.Create(proxyType); result = InvocationProxyFactory.CreateInvocation(proxyType, proxy); } else if (genericType == typeof(IInvocation <,>)) { proxyType = serviceType.GetGenericArguments()[0]; var interceptorType = serviceType.GetGenericArguments()[1]; IMethodInterceptor methodInterceptor = (IMethodInterceptor) _serviceProvider.GetRequiredService(interceptorType); proxy = DynamicProxy.CreateWithInterceptor(proxyType, methodInterceptor); result = InvocationProxyFactory.CreateInvocationWithInterceptor(proxyType, proxy, methodInterceptor); } return(result); }
public static IRepository <T> Create <T>(IRepository <T> repo) => DynamicProxy <IRepository <T> > .Create( repo, before : (sender, info) => Console.WriteLine($"Before operation {info.Name}"), after : (sender, info) => Console.WriteLine("After operation"), error : (sender, info) => Console.WriteLine("Error"));