Aspect Injector is an attribute-based framework for creating and injecting aspects into your .net assemblies.
> dotnet add package AspectInjector
"omnisharp.enableRoslynAnalyzers": true
[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!