The most common way to enable PostSharp in a C# or Visual Basic project is to import PostSharp targets (the file PostSharp-1.0.targets into the C# or VB project (*.csproj or *.vbproj).

The installer optionally enables PostSharp globally. Alternatively, you can enable or disable PostSharp manually, or on a per-project basis. See the section Enabling and Disabling PostSharp for details.

This section explains the default behavior of PostSharp targets, and explain how to override them.

Default Behavior

By default, PostSharp is invoked when the project directly or indirectly references the PostSharp.Public.dll assembly. If this dependency is detected, PostSharp will be invoked with the default project (Default.psproj).

The default project has the the following limitations:

The default behavior should fit the needs of most users. However, developers could need to override this behavior, principally on development environments.

We recommend that developers design their plug-ins so that their users can use the default MSBuild integration.

Explicitly providing a PostSharp project

If you cannot use the default PostSharp project (Default.psproj), there are many ways to tell PostSharp to use your own project.

Including a PostSharp project named after the C#/VB.NET project

The easiest way is to include in your C#/VB.NET project a PostSharp project file named after your C#/VB.NET project file.

The build process shall look for the following files:

  1. ProjectName . Configuration . Platform .psproj

  2. ProjectName . Configuration .psproj

  3. ProjectName .psproj

where ProjectName is the project file name without extension, Configuration is typically Debug or Release and Platform is typically AnyCPU.

Defining MSBuild properties

The second way to execute your own PostSharp project is to load the C#/VB project file using a text editor and to define the PostSharpProject property. This property should contain the path to the PostSharp project to be executed:

<PropertyGroup>
   <PostSharpProject>MyProject.psproj</PostSharpProject>
</PropertyGroup>

Overriding other properties influencing the PostSharp targets

PostSharp targets are parameterized by multiple properties described in section Recognized MSBuild Properties.

Overriding properties for a specific project

You can override PostSharp properties by editing your C# or VB project using a text editor and setting the properties as you would do for any other MSBuild property.

Overriding properties globally

Default values of properties are defined in the file PostSharp-1.0.targets, located in the installation directory of PostSharp. We do not recommend to modify this file, since you may loose changes once you will upgrade PostSharp to a newer version.

The best practice to change the default behavior of the PostSharp targets is to create a new file named PostSharp-1.0.Override.targets, located in the same directory as PostSharp-1.0.targets, and to specify the new values of your properties.

The following example shows how to force PostSharp to run the in-process MSBuild task instead of the standalone command-line utility:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
  <PropertyGroup>
     <PostSharpUseCommandLine>False</PostSharpUseCommandLine>
  </PropertyGroup>
</Project>