利用.NET Core类库System.Reflection.DispatchProxy实现简易Aop (2)

使用方法:

class Program { static void Main(string[] args) { // var poxy = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), new SamepleProxy()); // 或 //var poxy = (targetInterface)ProxyGenerator.Create(typeof(targetInterface), typeof(SamepleProxy)); // 或 var poxy = (targetInterface)ProxyGenerator.Create<targetInterface, SamepleProxy>(); poxy.Write("here is invoked by coreproxy"); } } public class SamepleProxy : IInterceptor { public object Intercept(object target, MethodInfo method, object[] parameters) { Console.WriteLine(parameters[0]); return null; } } public interface targetInterface { void Write(string writesome); }

总结一下就是,微软爸爸给我们的这个轮子还是即轻便又很好用的。
本文的实例代码可以在我的github上找到:https://github.com/ElderJames/CoreProxy

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpjgxs.html