I have never asked for any donations, but today, I ask you, please, consider donating Ukrainian Army.
Aspect Injector is an attribute-based framework for creating and injecting aspects into your .net assemblies.
> dotnet add package AspectInjector
- 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
- (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
- Unsafe methods are not supported and are silently ignored.
[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
}
}
[LogCall]
public void Calculate()
{
Console.WriteLine("Calculated");
}
Calculate();
$ dotnet run
Calling 'Calculate' method...
Calculated
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!");
}
}
[Initializable]
public class Target
{
}
var target = new Target() as IInitializable;
target.Init();
$ dotnet run
Initialized!