As with conventional compilers, warnings emitted by PostSharp, as well as those emitted by custom code running at build time in PostSharp, can be ignored (in that case they will not be displayed) or escalated into errors.
Warnings can be ignored either globally, using a project-wide setting, or locally for a given element of code. Warnings can be escalated only globally.
Ignoring or escalating warnings globally
There are several ways to ignore or escalate a warning for a complete project:
In Visual Studio, in the PostSharp tab of the project properties dialog. See Configuration for details.
By defining the
PostSharpDisabledMessagesorPostSharpEscalatedMessagesMSBuild properties. See Configuration and Configuring Projects Using MSBuild for details.
Note
The value * can be used to escalate all warnings into errors.
Treating warnings as errors with the standard MSBuild properties
PostSharp also reads the standard MSBuild properties that control the warnings of the C# compiler:
When the
TreatWarningsAsErrorsproperty isTrue, PostSharp escalates all its warnings, and all the warnings that aspects emit at build time, into errors. The only exception is the warningPS0131, which reports that the project contains no aspect.To keep the warnings of PostSharp as warnings in a project that treats the warnings of the compiler as errors, set the
PostSharpTreatWarningsAsErrorsproperty toFalse.The PostSharp message codes that are listed in the
WarningsAsErrorsproperty are escalated into errors, includingPS0131.The PostSharp message codes that are listed in the
WarningsNotAsErrorsproperty are excluded from the escalation caused byTreatWarningsAsErrorsorPostSharpTreatWarningsAsErrors. They are still escalated when they are listed explicitly inWarningsAsErrorsorPostSharpEscalatedMessages.
Important
Before PostSharp 2027.0, TreatWarningsAsErrors and WarningsAsErrors had no effect on the warnings of PostSharp. A project that sets TreatWarningsAsErrors now fails to build when PostSharp or an aspect emits a warning.
Ignoring warnings locally
Most warnings are related to a specific element of code. To disable a specific warning for a specific element of code, add the SuppressWarningAttribute custom attribute to that element of code, or to any enclosing element of code (for instance, adding the attribute to a type will make it effective for all members of this type).
You can create your own custom attribute derived from SuppressWarningAttribute and make it conditional on a compilation symbol by using the ConditionalAttribute custom attribute.