JCat.AOP.Runtime 1.0.3

dotnet add package JCat.AOP.Runtime --version 1.0.3
                    
NuGet\Install-Package JCat.AOP.Runtime -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JCat.AOP.Runtime" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JCat.AOP.Runtime" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="JCat.AOP.Runtime" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JCat.AOP.Runtime --version 1.0.3
                    
#r "nuget: JCat.AOP.Runtime, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JCat.AOP.Runtime@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JCat.AOP.Runtime&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=JCat.AOP.Runtime&version=1.0.3
                    
Install as a Cake Tool

JCat.AOP.Runtime

這個專案提供 AOP Runtime 的基礎元件與一個 MSBuild Task, 用來在編譯後以 IL 方式套用攔截邏輯。

內容與用途

  • AopExtensions
    • AddAop(IServiceProvider):初始化 AopServiceProvider 的靜態存取。
  • AopServiceProvider
    • 儲存全域 IServiceProvider,提供 GetRequiredService 取用 DI 服務。
  • AopInterceptorBase
    • 定義攔截器的基底介面(同步/非同步、泛型/非泛型)。
  • Attributes/AroundAttribute
    • 標記方法,指定攔截器型別。
  • Attributes/InterceptWithAttribute
    • 標記自訂 Attribute 類別,讓該 Attribute 指向攔截器型別。
  • BuildTask/AopCachePatchTask
    • 使用 Mono.Cecil 修改組件 IL:
      • 將被標記的方法複製為 $AopOriginal
      • 生成 $AopWrapper 包裝方法
      • 呼叫攔截器的 Around / AroundAsync
    • 透過 AroundAttribute 或 InterceptWithAttribute 解析攔截器型別。

基本使用方式

// 程式啟動時註冊
services.AddAop();

public class LoggingInterceptor : AopInterceptorBase
{
    public override Task AroundAsync(
        Func<object?[], Task> factory,
        string methodName,
        object?[] args,
        Attribute? attribute)
    {
        Console.WriteLine($"Before: {methodName}");
        return factory(args);
    }
}

public class ExampleService
{
    [Around(typeof(LoggingInterceptor))]
    public async Task DoWorkAsync()
    {
        await Task.Delay(10);
    }
}

Build Task 說明

AopCachePatchTask 會在編譯後以 IL 方式進行 patch。 當你使用 NuGet 套件 時,已透過 buildTransitive 自動掛載, 不需要在專案檔手動加入 UsingTask 或 Target。 只有在 ProjectReference 直接參考原始專案時才需要手動掛載。

範例:在專案檔掛載 Task

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  
  <ItemGroup>
    <ProjectReference Include="..\JCat.AOP.Runtime\JCat.AOP.Runtime.csproj" />
  </ItemGroup>

  
  <UsingTask
    TaskName="Aop.Runtime.BuildTask.AopCachePatchTask"
    AssemblyFile="$(OutputPath)JCat.AOP.Runtime.dll" />

  
  <Target Name="AopPatch" AfterTargets="Build">
    <AopCachePatchTask AssemblyPath="$(TargetPath)" />
  </Target>
</Project>

提醒:AssemblyFile 請指向包含 AopCachePatchTask 的組件輸出位置, 如果是 NuGet 套件,請改成對應的 $(Pkg... ) 路徑。

範例:使用 NuGet 套件時的寫法

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="JCat.AOP.Runtime" Version="1.0.3" />
  </ItemGroup>
</Project>

提醒:透過 NuGet 安裝會自動掛載 buildTransitive, 只要引用套件即可生效。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 152 1/22/2026
1.0.2 136 1/22/2026
1.0.1 133 1/22/2026
1.0.0 135 1/22/2026