C#面向切面編程之AspectCore用法詳解
寫在前面
AspectCore 是Lemon名下的一個(gè)國(guó)產(chǎn)Aop框架,提供了一個(gè)全新的輕量級(jí)和模塊化的Aop解決方案。面向切面也可以叫做代碼攔截,分為靜態(tài)和動(dòng)態(tài)兩種模式,AspectCore 可以實(shí)現(xiàn)動(dòng)態(tài)代理,支持程序運(yùn)行時(shí)在內(nèi)存中“臨時(shí)”生成 AOP 動(dòng)態(tài)代理類。
老規(guī)矩從 Nuget 安裝 AspectCore.Extensions.DependencyInjection 包。

代碼實(shí)現(xiàn)
using AspectCore.DynamicProxy;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Start...");
ProxyGeneratorBuilder proxyGeneratorBuilder = new ProxyGeneratorBuilder();
using (IProxyGenerator proxyGenerator = proxyGeneratorBuilder.Build())
{
Person p = proxyGenerator.CreateClassProxy<Person>();
Console.WriteLine(p.GetType().BaseType);
p.Say($"{Environment.NewLine} Hello World!");
}
Console.WriteLine("End");
Console.ReadLine();
}
}
public class CustomInterceptor : AbstractInterceptorAttribute
{
public async override Task Invoke(AspectContext context, AspectDelegate next)
{
try
{
Console.WriteLine("Before service call");
await next(context);
}
catch (Exception)
{
Console.WriteLine("Service threw an exception!");
throw;
}
finally
{
Console.WriteLine("After service call");
}
}
}
public class Person
{
[CustomInterceptor]
public virtual void Say(string msg)
{
Console.WriteLine("service calling..." + msg);
}
}
調(diào)用示例

如圖,代理類將Say方法包裹了起來。
如果修改一下CustomInterceptor 的Invoke方法,可以直接根據(jù)條件控制代碼的分支跳轉(zhuǎn)。
public class CustomInterceptor : AbstractInterceptorAttribute
{
public async override Task Invoke(AspectContext context, AspectDelegate next)
{
try
{
Console.WriteLine("Before service call");
if (false)
await next(context);
else
await Task.Delay(1000);
}
catch (Exception)
{
Console.WriteLine("Service threw an exception!");
throw;
}
finally
{
Console.WriteLine("After service call");
}
}
}
運(yùn)行代碼 Person中的Say方法本體就被跳過了:

到此這篇關(guān)于C#面向切面編程之AspectCore用法詳解的文章就介紹到這了,更多相關(guān)C# AspectCore內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# WinForms程序調(diào)用Python腳本的完整代碼案例
本文介紹了一個(gè)完整的C# WinForms應(yīng)用程序,該應(yīng)用程序通過下拉框選擇并執(zhí)行不同的Python腳本,文章詳細(xì)描述了項(xiàng)目結(jié)構(gòu)、Python腳本準(zhǔn)備、C#代碼實(shí)現(xiàn)、使用說明和配置說明,并提供了功能特點(diǎn)和擴(kuò)展建議,需要的朋友可以參考下2025-11-11
C#實(shí)現(xiàn)計(jì)算器精簡(jiǎn)版
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)計(jì)算器精簡(jiǎn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C#中存儲(chǔ)當(dāng)前項(xiàng)目設(shè)置的步驟詳解
我們?cè)诰帉戃浖臅r(shí)候總有一些配置數(shù)據(jù)需要保存,比如用戶選擇的偏好設(shè)置,又如軟件所用到的數(shù)據(jù)庫文件等,我們有很多中方式都可以保存,本文給大家介紹了C#中存儲(chǔ)當(dāng)前項(xiàng)目設(shè)置的詳細(xì)步驟,需要的朋友可以參考下2026-02-02
C#使用SemaphoreSlim實(shí)現(xiàn)并發(fā)控制與限流策略的實(shí)戰(zhàn)指南
在現(xiàn)代應(yīng)用中(如爬蟲、并發(fā)請(qǐng)求、數(shù)據(jù)庫連接池、異步任務(wù)處理),我們常常需要限制同時(shí)執(zhí)行的任務(wù)數(shù)量,以避免過載或資源競(jìng)爭(zhēng),在 C# 中,最簡(jiǎn)潔高效的解決方案之一就是SemaphoreSlim,本文就給大家介紹了C#使用SemaphoreSlim實(shí)現(xiàn)并發(fā)控制與限流策略的實(shí)戰(zhàn)指南2025-11-11
C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程(附源碼)
這篇文章主要介紹了C# Winform調(diào)用百度接口實(shí)現(xiàn)人臉識(shí)別教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
C#使用FluentHttpClient實(shí)現(xiàn)請(qǐng)求WebApi
FluentHttpClient 是一個(gè)REST API 異步調(diào)用 HTTP 客戶端,調(diào)用過程非常便捷,下面我們就來學(xué)習(xí)一下C#如何使用FluentHttpClient實(shí)現(xiàn)請(qǐng)求WebApi吧2023-12-12
C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了如何通過C#實(shí)現(xiàn)word和pdf格式互轉(zhuǎn)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-10-10
ItemsControl 數(shù)據(jù)綁定的兩種方式
這篇文章主要介紹了ItemsControl 數(shù)據(jù)綁定的兩種方式,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

