| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot <Operation> the value of <Member>: <Reason> |
Cause
Some sections of postsharp.config, such as the logging profiles of the Logging section, describe CLR objects in XML, as Including CLR Objects in Configuration explains. PostSharp creates the objects during the build and sets their properties and fields with reflection: an XML attribute or a child element sets the member of the same name, and the items of a collection are added to the collection that the property returns.
PostSharp reports PS0255 when setting or reading the member throws an exception. <Operation> is set or get, <Member> is the property or the field, and <Reason> is the message of the exception. The most common cause is a read-only property, whose setter does not exist. A setter can also reject the value. The location of the message is the XML attribute or element in postsharp.config.
In the following example, the Orders profile tries to set its backend. The Backend property of a profile is read-only: the backend is a run-time option, which the application sets.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
<Profiles>
<LoggingProfile Name="Orders">
<DefaultOptions>
<LoggingOptions Level="Info" />
</DefaultOptions>
<!-- The backend is a run-time option: the Backend property of a profile cannot be set. -->
<Backend>
<ConsoleLoggingBackend xmlns="clr-namespace:PostSharp.Patterns.Diagnostics.Backends.Console;assembly:PostSharp.Patterns.Diagnostics" />
</Backend>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0255_Error;
public class OrderService
{
// The aspect uses the Orders profile of postsharp.config.
[Log( "Orders" )]
public void PlaceOrder( string product, int quantity )
{
Console.WriteLine( $"Ordered {quantity} x {product}." );
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
new OrderService().PlaceOrder( "Coffee", 2 );
}
}
error PS0255: postsharp.config(10,10): Cannot set the value of PostSharp.Patterns.Diagnostics.LoggingProfile.Backend: Property set method not found.
Resolution
- Remove the attribute or the element that sets a read-only property. Check in the API reference which properties of the type can be set.
- When the setter rejects the value, change the value:
<Reason>says what is wrong. - Set run-time options, such as the logging backend, in the code of the application. See Formatting Log Records.
In the following example, the profile sets only build-time options, and Program.Main sets the backend.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
<Profiles>
<LoggingProfile Name="Orders">
<DefaultOptions>
<LoggingOptions Level="Info" />
</DefaultOptions>
<!-- The backend is set by the application, in Program.Main. -->
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0255_Fixed;
public class OrderService
{
// The aspect uses the Orders profile of postsharp.config.
[Log( "Orders" )]
public void PlaceOrder( string product, int quantity )
{
Console.WriteLine( $"Ordered {quantity} x {product}." );
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
new OrderService().PlaceOrder( "Coffee", 2 );
}
}
Info | Trace | OrderService.PlaceOrder("Coffee", 2) | Starting.
Ordered 2 x Coffee.
Info | Trace | OrderService.PlaceOrder("Coffee", 2) | Succeeded.
See Also
Reference
Other Resources
Including CLR Objects in Configuration
Formatting Log Records
Configuration File Schema Reference