Alternatives for AOP

Today’s aspect-oriented frameworks all rely on one of the following approaches:

  • MSIL Rewriting is the process of modifying the binary assembly during the build process. An additional step is added to this process just after the C# compiler. This approach was pioneered by PostSharp when the C# compiler was a black box. It is now considered obsolete, but many tools still rely on it.

  • Roslyn-based frameworks interact directly with the compiler without requiring an additional process. They are based on official Roslyn extension points such as analyzers and code generators, plus unofficial extension points added by Metalama.Compiler, an open-source fork of Roslyn, allowing arbitrary code transformations. Metalama is the only framework in this category at the moment. Roslyn-based frameworks can give you real-time feedback as you are typing, while MSIL-based ones require you to rebuild.

  • Middleware-based frameworks generally rely on a dependency injection framework and generate dynamic proxies at runtime. They are limited to intercepting interface methods and adding new interfaces to types.

Here is an overview of the main aspect-oriented frameworks available for .NET in 2024. This list is followed by a table comparing their features.

PostSharp

PostSharp, first launched in 2008, was the first complete implementation of AOP concepts in .NET. It is based on MSIL rewriting. PostSharp became a source of inspiration for several MSIL-based AOP frameworks.

PostSharp includes a broad set of ready-made aspects. It has complete documentation.

It comes with a Visual Studio extension that provides visibility into the transformations performed by aspects.

Metalama

Metalama, built by the same team as PostSharp and first launched in 2023, is PostSharp’s successor. Based on Roslyn, Metalama works both at design time (within the IDE) and at compile time. It is today’s most complete implementation of aspect-oriented principles.

Metalama uses a C#-to-C# template language coined T#. T# is 100% C#-compatible, so you can get full IntelliSense support with any IDE.

Since Metalama generates C# and not MSIL, you can preview and even debug the code generated by your aspects.

Metalama shares the same Visual Studio extension as PostSharp.

AspectInjector

Like PostSharp, AspectInjector is based on MSIL rewriting.

While far from PostSharp in terms of features, AspectInjector supports most code overriding and introduction features expected from an AOP framework.

Rougamo

Rougamo is another compile-time AOP framework based on MSIL rewriting. Its code transformation abilities are limited. It implements an AspectJ-inspired pointcut mechanism to select code to be modified.

AspectCore

AspectCore is an aspect-oriented framework based on dynamic proxies. This approach operates at runtime by generating a proxy type between the consumer and the implementation of an interface. It works only with components served by a dependency injection framework.

Fody

Fody is an extensible tool for weaving .NET assemblies. It is not an aspect framework in itself, but some plug-ins allow for PostSharp-style decoration of methods, allowing for the implementation of some simple aspects.

Fody has a long list of plug-ins that implement specific code transformations. Because these transformations must be coded directly in MSIL and not in C#, Fody does not fully qualify as an aspect-oriented framework.

Comparison

 MetalamaPostSharpAspectInjectorRougamoAspectCore
TechnologyRoslynMSILMSILMSILDynamic Proxies
Override virtual membersYesYesYesYesYes
Override non-virtual membersYesYesYesYesNo
Implement interfacesYesYesYesNoYes
Introduce new membersYesYesYesNoNo
Reference introduced members from source codeYesNoNoNoNo
Allocationless context passingYesNoYesNoNo
IDE: Aspect ExplorerYesYesNoNoNo
IDE: CodeLensYesYesNoNoNo
View/Debug Generated C#YesNoNoNoNo
Large library of pre-built aspectsYesYesNoNoNo