| Severity | Error |
| Reported by | PostSharp |
| Message | Invalid expression "<Expression>": cannot convert "<Value>" to a <Type>. Expected values: <ExpectedValues>. |
Cause
Several properties of PostSharp are switches: for example WriteWeavingSymbols, SignAssembly or Deterministic. PostSharp receives them from MSBuild properties such as PostSharpWriteWeavingSymbols, from the PostSharpProperties MSBuild property, or from a Property element of a postsharp.config file, and the Condition attributes of configuration files are switches too. When PostSharp evaluates a switch, it accepts True, False, in any case, or an empty value, which means False. Any other value, such as Yes, 1 or On, makes PostSharp report PS0135.
<Expression> is the expression that PostSharp evaluated, usually a reference to a property such as {$WriteWeavingSymbols}, and <Value> the text it evaluated to. <Type> is always bool, and <ExpectedValues> is True | False. The message has no location in the source code.
When the property configures a task of the PostSharp compiler, the task cannot be created and PostSharp also reports PS0084, which stops the build.
In the following example, the @Property directive of the test framework of this documentation sets the WriteWeavingSymbols property to Yes, as PostSharpProperties would in a project file.
// @Property(WriteWeavingSymbols=Yes)
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0135_Error;
// The property of the directive above is set to Yes, which is not a Boolean value.
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}
public class OrderService
{
[Log]
public void SubmitOrder() => Console.WriteLine( "Order submitted." );
}
public static class Program
{
public static void Main()
{
new OrderService().SubmitOrder();
}
}
error PS0084: The project could not be initialized because of the previous errors.
error PS0135: Invalid expression "{$WriteWeavingSymbols}": cannot convert "Yes" to a bool. Expected values: True | False.
Resolution
Find where the property is set, from its name in <Expression>, and set it to True or False:
- In the project file or in a
Directory.Build.propsfile, for an MSBuild property such as<PostSharpWriteWeavingSymbols>True</PostSharpWriteWeavingSymbols>, or in thePostSharpPropertiesproperty. - In a
postsharp.configfile, for aPropertyelement or aConditionattribute. - On the command line, for a property passed with
-p:.
In the following example, the property is set to True.
// @Property(WriteWeavingSymbols=True)
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0135_Fixed;
// The property of the directive above is set to True, which is a Boolean value.
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args ) => Console.WriteLine( $"Entering {args.Method.Name}." );
}
public class OrderService
{
[Log]
public void SubmitOrder() => Console.WriteLine( "Order submitted." );
}
public static class Program
{
public static void Main()
{
new OrderService().SubmitOrder();
}
}
Entering SubmitOrder.
Order submitted.
See Also
Other Resources
Well-Known PostSharp Properties
Well-Known MSBuild Properties
Configuring Projects Using MSBuild