Enable/Disable Aspect at Runtime

ON's Avatar


17 May, 2017 12:12 AM


I was wondering if there was a way I could enable/disable an aspect at runtime. I have a MethodBoundryAspect that runs all over my project, if for some reason i would like to disable it what would be the best way to go about doing that without disrupting too much? Since this Aspect runs against all of the methods in multiple projects I would need something that wouldn't create a performance hit. I was wondering if there was a PostSharp configuration value that could be changed to enable/disable the aspect.

Here is an example of my aspect:

public sealed class MyAspect : OnMethodBoundaryAspect
    public override void OnEntry(MethodExecutionArgs args)
             //Logic here to check if the Aspect is enable or disabled

    public override void OnExit(MethodExecutionArgs args)
             //Logic here to check if the Aspect is enable or disabled

I define it the project's AssemlyInfo.cs file like so

  [assembly: MyAspect]


  1. Support Staff 1 Posted by PostSharp Techn... on 17 May, 2017 03:07 PM

    PostSharp Technologies's Avatar


    Your example is the only way how to enable/disable an aspect on runtime. Unfortunately there is no way how to completely avoid runtime overhead of a disabled aspect without rebuilding the application.

    You can mitigate the runtime overhead by taking advantage of Aspect Optimizer. Aspect Optimizer optimizes out unused fields of MethodExecutionArgs. You may also want to try pre-process as much as possible in CompileTimeInitialize which runs during compilation.

    If you want to traces method execution, you may try revamped PostSharp.Patterns.Diagnostic Log aspect in PostSharp 5.0. Unfortunately we don't have complete documentation yet for the feature, but you can take a look at this knowledge page.


  2. Support Staff 2 Posted by PostSharp Techn... on 26 Jun, 2017 11:29 AM

    PostSharp Technologies's Avatar


    We are going to close this request as we believe it was solved. Please feel free to reopen the discussion if you need more help.

    Best regards,
    PostSharp Team

  3. PostSharp Technologies closed this discussion on 26 Jun, 2017 11:29 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac