您现在的位置是:网站首页> 编程资料编程资料
.NET Core类库System.Reflection.DispatchProxy实现简易Aop的方法_实用技巧_
2023-05-24
342人已围观
简介 .NET Core类库System.Reflection.DispatchProxy实现简易Aop的方法_实用技巧_
前言
aop即是面向切面编程,众多Aop框架里Castle是最为人所知的,另外还有死去的Spring.NET,当然,.NET Core社区新秀AspectCore在性能与功能上都非常优秀,已经逐渐被社区推崇和有越来越多的人使用。感谢柠檬同学的礼物!
如果大家出于自身需求或者学习,想实现一个Aop,是不是觉得一来就要使用Emit去做?最近我了解到了System.Reflection.DispatchProxy这个corefx类库,已经实现了动态代理功能。
1|1System.Reflection.DispatchProxy
下面演示一下它的使用方法:
class Program { static void Main(string[] args) { //创建代理类,并把SampleProxy作为拦截器注入 var sampleProxy = (targetInterface)SampleProxy.Create(); //执行接口方法 sampleProxy.Write("here is invoke by proxy"); } } //需要被生成代理实例的接口 public interface targetInterface { //这个方法会被代理类实现 void Write(string writesomeshing); } public class SampleProxy : DispatchProxy { /// /// 拦截调用 /// /// 所拦截的方法信息 /// 所拦截方法被传入的参数指 /// protected override object Invoke(MethodInfo targetMethod, object[] args) { Console.WriteLine(args[0]); return null; } } 2|0改造成一款简易AOP
2|1为什么?
System.Reflection.DispatchProxy只有一个Api,就是objecct Create
2|2改造方法
幸好,在那个issue下,issue作者提供了一个解决方案,就是用反射来构造这个泛型方法。我还在这基础上,封装了一下,加入了传入拦截器实例和传入拦截器构造方法参数的功能。
////// 拦截器接口 /// public interface IInterceptor { ////// 拦截器调用 /// /// 代理实例 /// 所拦截的方法 /// 所拦截方法传入的参数值 ///返回值会传递给方法返回值 object Intercept(object target, MethodInfo method, object[] parameters); }
拦截器要实现这个接口,下面是对DispatchProxy的封装,实现更多创建代理实例的方法
public class ProxyGenerator : DispatchProxy { private IInterceptor interceptor { get; set; } /// /// 创建代理实例 /// /// 所要代理的接口类型 /// 拦截器 /// 代理实例 public static object Create(Type targetType, IInterceptor interceptor) { object proxy = GetProxy(targetType); ((ProxyGenerator)proxy).CreateInstance(interceptor); return proxy; } /// /// 创建代理实例 /// /// 所要代理的接口类型 /// 拦截器类型 /// 拦截器构造函数参数值 /// 代理实例 public static object Create(Type targetType, Type interceptorType, params object[] parameters) { object proxy = GetProxy(targetType); ((ProxyGenerator)proxy).CreateInstance(interceptorType, parameters); return proxy; } /// /// 创建代理实例 TTarget:所要代理的接口类型 TInterceptor:拦截器类型 /// /// 拦截器构造函数参数值 /// 代理实例 public static TTarget Create(params object[] parameters) where TInterceptor : IInterceptor { var proxy = GetProxy(typeof(TTarget)); ((ProxyGenerator)proxy).CreateInstance(typeof(TInterceptor), parameters); return (TTarget)proxy; } private static object GetProxy(Type targetType) { var callexp = Expression.Call(typeof(DispatchProxy), nameof(DispatchProxy.Create), new[] { targetType, typeof(ProxyGenerator) }); return Expression.Lambda>(callexp).Compile()(); } private void CreateInstance(Type interceptorType, object[] parameters) { var ctorParams = parameters.Select(x => x.GetType()).ToArray(); var paramsExp = parameters.Select(x => Expression.Constant(x)); var newExp = Expression.New(interceptorType.GetConstructor(ctorParams), paramsExp); this.interceptor = Expression.Lambda>(newExp).Compile()(); } private void CreateInstance(IInterceptor interceptor) { this.interceptor = interceptor; } protected override object Invoke(MethodInfo method, object[] parameters) { return this.interceptor.Intercept(method, parameters); } } 2|3使用方法
class Program { static void Main(string[] args) { var poxy1 = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), new SampleProxy("coreproxy1")); poxy1.Write("here was invoked"); //---> "here was invoked by coreproxy1" var poxy2 = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), typeof(SampleProxy), "coreproxy2"); poxy2.Write("here was invoked"); //---> "here was invoked by coreproxy2" var poxy3 = ProxyGenerator.Create("coreproxy3"); poxy3.Write("here was invoked"); //---> "here was invoked by coreproxy3" } } public class SampleProxy : IInterceptor { private string proxyName { get; } public SampleProxy(string name) { this.proxyName = name; } public object Intercept(MethodInfo method, object[] parameters) { Console.WriteLine(parameters[0] + " by " + proxyName); return null; } } public interface targetInterface { void Write(string writesome); } 3|0总结
总结一下就是,微软爸爸给我们的这个轮子还是即轻便又很好用的。
本文的实例代码可以在我的github上找到:https://github.com/ElderJames/CoreProxy
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
相关内容
- WPF实现进度条实时更新效果_实用技巧_
- Asp.Net Core使用SignalR进行服务间调用方法示例_实用技巧_
- .NET微信小程序用户数据的签名验证和解密代码_实用技巧_
- Asp.Net Core轻松学之利用日志监视进行服务遥测详解_实用技巧_
- .net core并发请求发送HttpWebRequest的坑解决_实用技巧_
- Aspose.Cells组件导出excel文件_实用技巧_
- C#使用Aspose.Cells导出excel_实用技巧_
- 利用Aspose.Cells实现万能导出功能_实用技巧_
- 使用Aspose.Cells实现导入导出_实用技巧_
- mvc实现图片验证码功能_实用技巧_
