AspectInjector.NET
2.8.3
dotnet add package AspectInjector.NET --version 2.8.3
NuGet\Install-Package AspectInjector.NET -Version 2.8.3
<PackageReference Include="AspectInjector.NET" Version="2.8.3" />
paket add AspectInjector.NET --version 2.8.3
#r "nuget: AspectInjector.NET, 2.8.3"
// Install AspectInjector.NET as a Cake Addin #addin nuget:?package=AspectInjector.NET&version=2.8.3 // Install AspectInjector.NET as a Cake Tool #tool nuget:?package=AspectInjector.NET&version=2.8.3
<h3><span style="color: #ff0000;"><strong>I have never asked for any donations, but today, I ask you, please, consider donating Ukrainian Army.<br /></strong></span></h3> <h3><span style="color: #ff0000;"><strong>You can find official ways to do it <a href="https://bank.gov.ua/en/news/all/natsionalniy-bank-vidkriv-spetsrahunok-dlya-zboru-koshtiv-na-potrebi-armiyi">here</a> or you can donate to the biggest charity organization <a href="https://www.comebackalive.in.ua/">here</a></strong></span></h3> <h3><span style="color: #ff0000;"><strong>People need to be alive to create open source projects!</strong></span></h3>
<img src="https://raw.githubusercontent.com/pamidur/aspect-injector/master/package.png" width="48" align="right"/>Aspect Injector
Aspect Injector is an attribute-based framework for creating and injecting aspects into your .net assemblies.
Project Status
Download
> dotnet add package AspectInjector
Features
- Compile-time injection - works with Blazor and AOT
- Injecting Before, After and Around (wrap) Methods, Constructors, Properties and Events
- Injecting Interface implementations
- Supports any project that can reference netstandard2.0 libraries, see here
- Debugging support
- Roslyn analyzers for your convenience (only c# currently)
- Can work DI/IoC frameworks #166
Requirements
- (semi-optional) Nuget 5.0+ for transient build feature. All modern versions of VS and dotnetsdk have it. (If you still use project.json for some reason - make sure you add AspectInjector to all projects in the solution)
- (optional) For analyzers to work in VSCode, don't forget to enable
"omnisharp.enableRoslynAnalyzers": true
Known Issues / Limitations
- Unsafe methods are not supported and are silently ignored.
Simple advice
Create an aspect with simple advice:
[Aspect(Scope.Global)]
[Injection(typeof(LogCall))]
public class LogCall : Attribute
{
[Advice(Kind.Before)] // you can have also After (async-aware), and Around(Wrap/Instead) kinds
public void LogEnter([Argument(Source.Name)] string name)
{
Console.WriteLine($"Calling '{name}' method..."); //you can debug it
}
}
Use it:
[LogCall]
public void Calculate()
{
Console.WriteLine("Calculated");
}
Calculate();
Result:
$ dotnet run
Calling 'Calculate' method...
Calculated
Simple mixin
Create an aspect with mixin:
public interface IInitializable
{
void Init();
}
[Aspect(Scope.PerInstance)]
[Injection(typeof(Initializable))]
[Mixin(typeof(IInitializable))]
public class Initializable : IInitializable, Attribute
{
public void Init()
{
Console.WriteLine("Initialized!");
}
}
Use it:
[Initializable]
public class Target
{
}
var target = new Target() as IInitializable;
target.Init();
Result:
$ dotnet run
Initialized!
Product | Versions 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. |
-
net8.0
- No dependencies.
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 |
---|---|---|
2.8.3 | 1,017 | 8/13/2024 |