Skip to content
An AOP library that allows you to inject cross cutting concerns in an easy manner.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Build status

Aspectus is an AOP library that allows you to inject cross cutting concerns in an easy manner.

Setting Up the Library

Aspectus relies on Canister in order to hook itself up. In order for this to work, you must do the following at startup:

Canister.Builder.CreateContainer(new List<ServiceDescriptor>())

The RegisterAspectus function is an extension method that registers it with the IoC container. When this is done, Aspectus is ready to use.

Basic Usage

The way that Aspectus handles AOP is by doing code generation using Roslyn. As such you're writing C# codes for the most part. Start by implementing a class that inherits from IAspect:

public interface IExample
    string MySecretData { get; set; }

public class TestAspect : IAspect
    public TestAspect()
        AssembliesUsing = new List<MetadataReference>();
        foreach (var DLL in new DirectoryInfo(@"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0.0\")
                                                    .Where(x => !DontLoad.Contains(x.Name)))
            var TempAssembly = MetadataReference.CreateFromFile(DLL.FullName);

    public ICollection<MetadataReference> AssembliesUsing { get; private set; }

    public ICollection<Type> InterfacesUsing => new Type[] { typeof(IExample) };

    public ICollection<string> Usings => new string[] { };

    private string[] DontLoad = {

    public void Setup(object value)
        var ExampleValue = value as IExample;
        if (ExampleValue != null)
            ExampleValue.MySecretData = "BLAH";

    public string SetupDefaultConstructor(Type baseType)
        return "";

    public string SetupEndMethod(MethodInfo method, Type baseType, string returnValueName)
        return "";

    public string SetupExceptionMethod(MethodInfo method, Type baseType)
        return "";

    public string SetupInterfaces(Type type)
        return "public string MySecretData{get; set;}";

    public string SetupStartMethod(MethodInfo method, Type baseType)
        return "";

You will notice a couple of things with this code. The first is the most annoying part which is the assembly reference. Thanks to .Net Core's approach to assembly referencing it pushes everything into a dotnet folder. On Linux, Mac, etc. it will be in a different location. On top of that there are a number of DLLs that do not have metadata associated with them and Roslyn breaks if you try to compile against them. As such, if you're using this, you will probably prefer doing a more specified load of just the DLLs that you need and not everything in the NetCore.App directory. Similarly you will need to make sure you are pointing to the correct version. The above is just pointing to 1.0.0, change it accordingly if you wish to target 1.1 or 1.0.1 or something else.

The next bit of code to notice is that you must define any special interfaces you wish the newly generated type to inherit from. It is not possible to specify a base class as the type that is being generated will already be declared based on the type requested. Third property to notice is the Usings property. In the case above we aren't using any but if you would like to add any, that is where you do it.

There are then a couple of functions in this class. SetupDefaultConstructor allows you to inject code into the default constructor for the type that you are generating. Any class setup code should be handled here. SetupInterfaces is where you would declare any functions, properties, or fields needed in order to implement the interfaces that you specified earlier. In the example above it returns the MySecretData property defined in the IExample interface. The SetupStartMethod is where you would return any code that you wish to inject at the beginning of the method. SetupEndMethod similarly is where you return any code that you wish to inject at the end of a method call. And lastly there is SetupExceptionMethod. This is where you would return any code that you wish to inject when an exception is thrown by the method.

After the code is constructed, the user calls the Create function on the Aspectus class:

public class AOPTestClass
    public virtual string A { get; set; }

    public virtual int B { get; set; }

    public virtual float C { get; set; }

    public virtual List<string> D { get; set; }


var Test = Canister.Builder.Bootstrapper.Resolve<Aspectus>();
var Item = (AOPTestClass)Test.Create(typeof(AOPTestClass));

This will construct a new instance of AOPTestClass and return it to the user. Before it returns it, it will call into your aspect one last time calling the Setup method, passing in the object created. When it does that you can modify the object however you would like prior to it being returned to the end user. From there the end user can modify, use, and debug their object without any issue.

AOP Modules

While you can just create an Aspectus object and call Setup to implement items, you can actually do this when the the object is created for the first time by implementing the IAOPModule interface. By doing this you can tell the system to find and load your setup code all at once instead of doing it in chunks.


The library is available via Nuget with the package name "Aspectus". To install it run the following command in the Package Manager Console:

Install-Package Aspectus

Build Process

In order to build the library you will require the following:

  1. Visual Studio 2015 with Update 3
  2. .Net Core 1.0 SDK

Other than that, just clone the project and you should be able to load the solution and build without too much effort.

You can’t perform that action at this time.