Popularity
4.8
Growing
Activity
5.9
Declining
388
41
64

Programming language: C#
License: Apache License 2.0
Latest version: v2.4.1

Aspect Injector alternatives and similar packages

Based on the "Assembly Manipulation" category.
Alternatively, view Aspect Injector alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of Aspect Injector or a related project?

Add another 'Assembly Manipulation' Package

README

Aspect Injector

Aspect Injector is an attribute-based framework for creating and injecting aspects into your .net assemblies.

Project Status

Nuget Nuget Pre GitHub (Pre-)Release Date GitHub last commit

Build Status Samples 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 implementaions
  • Supports any project that can reference netstandard2.0 libraries, see here
  • Debugging support
  • Roslyn analyzers for your convenience (only c# currently)

Check out [samples](samples) and [docs](docs)

Requirements

  • .NetCore runtime 2.1.6+ installed on your machine (your projects can be anything that can reference netstandard2.0)
  • (optional) For analyzers to work in VSCode, don't forget to enable "omnisharp.enableRoslynAnalyzers": true

Known Issues / Limitations

  • 'Edit and Continue' feature in VS is not working at the moment with .netcore3+ #138
  • Unsafe methods are not supported and are silently ignored.
  • Until Nuget v5 (with transient build feature) you need to refrecence AspectInjector into every project in your solution. Thus, if VisualStudio >= 2019 && CoreSDK >= 2.1.602 no worries about references else reference AspectInjector directly to projects where aspects are defined or used

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!