I thought we would close out this week with a look at the Visual Studio add-ins PostSharp provides for us.
When applying aspects at a class level or assembly level it really hides which methods are going to be affected by an aspect. This is even worse when multiple aspects are applied.
PostSharp is nice enough to provide a visual aid in determining which methods are affected by which aspects, the Aspect Browser. Select the PostSharp Browser from the View menu.
This window is split into two panes. The top pane, Aspect Browser, contains all aspects in the solution. The grouping is defaulted to namespace but you can adjust this in the settings or by right clicking on an object. In our project there is only one namespace, PostSharpDemo1. If you add a new project and an aspect class, you won't see any changes. Only when the aspect is applied will you see it in the browser.
The Aspect Browser displays information generated by PostSharp at build time so you will have to rebuild your project(s) to see the changes made in your code.
Selecting an aspect will change the Affected Code pane (bottom pane) showing all of the methods affected by the selected aspect. The Affected Code pane is also grouped and sorted by namespace as the default. You can group and sort the Affected Code independently from the Aspect Browser.
Right clicking an item will bring up a context menu that provides options for sorting and grouping as well as going to the definition of that item. Double clicking an item will also take you to the definition.
Often we need to see all aspects that a method is being affected by. Again, PostSharp has us covered. If you haven’t noticed by now then take a look at your code. PostSharp enhances methods which are affected by aspects by adding a thick underlining.
Hovering over the method name will bring up a tool-top box that lists not only which aspects affect the method but also at which level the aspect is applied.
The tool-tip provides links that can be used to navigate to the aspect definitions and aspect declarations.
PostSharp is a very sharp tool. These add-ins are our gloves and prevent us from cutting ourselves (maybe). They can have a huge impact when using PostSharp in your development weather you’re testing multicast declarations or troubleshooting aspect issues.