5

I can specify the SkipPostSharp constant to ensure a project is excluded from the list of projects PS processes. I want to do it the other way around though. I want PS to assume it shouldn't process anything that I don't specifically tell it to.

Is this achievable?

Brandon Moore
  • 8,590
  • 15
  • 65
  • 120

1 Answers1

15

There are three conditions for a project to be automatically processed by PostSharp:

  1. PostSharp has been installed using the setup program.
  2. The project has a reference (direct or indirect) to PostSharp.dll.
  3. The MSBuild property SkipPostSharp is different than true and the compilation symbol SkipPostSharp is undefined.

The third condition is what becomes false when you disable PostSharp by checking the option in VS project properties.

You could disable PostSharp by default by setting the SkipPostSharp=True property by default. This can be achieved by creating a file named PostSharp.Custom.targets in one of the parent directories of your projects, with the following content:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
     <SkipPostSharp Condition="'$(SkipPostSharp)'==''">True</SkipPostSharp>
  </PropertyGroup>
</Project>

Then, in every project where PostSharp is actually needed, you would need to define the property SkipPostSharp=False. You will have to do that using a text editor, because the project property tab only allows to set the property to True or to undefine it.

Gael Fraiteur
  • 6,759
  • 2
  • 24
  • 31
  • Just for clarification... If I have a directory called 'SourceCode' that all my project directories reside in, then I could create the PostSharp.Custom.targets file in that folder and any new projects that other people create will *not* automatically use post sharp even if they reference other projects that do. Is that correct? – Brandon Moore Mar 23 '12 at 23:05
  • Assuming their projects go into the SourceCode folder, of course. – Brandon Moore Mar 23 '12 at 23:06
  • 1
    This is documented here: http://doc.sharpcrafters.com/postsharp-2.1/Content.aspx/PostSharp-2.1.chm/html/b1071f40-36ec-488c-a418-c9f19e3c8dbc.htm. Basically, PostSharp will look in parent directories of the project file, so you the file can be shared among several projects and solutions. – Gael Fraiteur Mar 27 '12 at 07:55
  • Thanks Gael... for being awesome. – Brandon Moore Mar 28 '12 at 00:53
  • If you want to edit the project file in VisualStudio, you can unload the project and then the option should be available to edit. Also, if you install the VisualStudio Power Command extension, it adds a context menu to edit the project file (it simply automates the unloading of the project for you) – Chris Feb 01 '13 at 19:12
  • I've tried adding the 'PostSharp.Custom.targets' file and put in the text as per your answer but this has no effect and PostSharp is still active. Is there anything else I'd need to do (change Build Action etc.) or does this no longer apply to the latest version of PostSharp? – MJF May 09 '16 at 12:46
  • SkipPostSharp should still work in the latest version. It's sometimes difficult to debug MSBuild. Try to deliberately make your PostSharp.Custom.targets file an invalid XML file to see if MSBuild even tries to load it. – Gael Fraiteur May 09 '16 at 15:25