基于DispatchProxy打造自定义AOP组件
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                基于DispatchProxy打造自定义AOP组件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                DispatchProxy是微軟爸爸編寫的一個代理類,基于這個,我擴展了一個AOP組件
暫時不支持依賴注入構造方法,感覺屬性注入略顯麻煩,暫時沒打算支持
基于特性的注入流程
[AttributeUsage(AttributeTargets.Method)]internal class TestAttribute:AopAttribute{protected override Task BeforeAsync(MethodInfo targetMethod, object[] args){Console.WriteLine($"BeforeAsync {targetMethod.Name}");return base.BeforeAsync(targetMethod, args);}protected override Task AfterAsync(MethodInfo targetMethod, object[] args){Console.WriteLine($"AfterAsync {targetMethod.Name}");return base.AfterAsync(targetMethod, args);}}編寫接口
public interface ITest{[Test]void TestVoid();[Test]int TestInt();[Test]Task TestAsync();[Test]Task<int> TestIntAsync();}?
整體結構是依賴注入,再實現一個實現類就好
特性打在接口的方法上
剛開坑,暫時只支持特性對應方法
調用
services.AddNCoreCoderAop<ITest, Test>(ServiceLifetime.Transient);方法支持同步和異步兩種
支持Singleton、Scoped、Transient三種生命周期,默認生命周期是Singleton
項目地址:
https://github.com/htrlq/NCoreCoder.Framework
轉載于:https://www.cnblogs.com/NCoreCoder/p/11492755.html
總結
以上是生活随笔為你收集整理的基于DispatchProxy打造自定义AOP组件的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: sendkeys安装包_利用Window
- 下一篇: 太极熊猫3服务器维护中,太极熊猫3月10
