| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot set property '<PropertyName>' because it is sealed. |
Cause
A Property element of a postsharp.config file with the Sealed="true" attribute defines a property that no later Property element can change. See Configuration File Schema Reference. This lets the configuration file at the root of a repository impose a value on every project below it.
PostSharp loads the postsharp.config files from the root directory down to the directory of the project, and their Property elements in the order of the file. It reports PS0207 on a Property element that sets a property that an earlier element sealed, and keeps the sealed value. Since the error occurs while PostSharp loads the configuration of the project, it also reports PS0084 and stops the build.
In the following example, the configuration file of the parent directory seals the LoggingEnabled property:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The configuration file of the parent directory seals the property: no other file can change it. -->
<Property Name="LoggingEnabled" Value="True" Sealed="true" />
</Project>
The configuration file of the project tries to change it:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The configuration file of the project tries to change the sealed property. -->
<Property Name="LoggingEnabled" Value="False" />
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0207_Error;
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
public class OrderService
{
[Log]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Order placed for {product}." );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
error PS0084: The project could not be initialized because of the previous errors.
error PS0207: postsharp.config(4,4): Cannot set property 'LoggingEnabled' because it is sealed.
Resolution
- Remove the
Propertyelement that changes the sealed property, so that the project uses the sealed value. - When the project must have another value, remove the
Sealedattribute from the element that seals the property, or move that element to a configuration file that does not apply to this project.
In the following example, the configuration file of the project no longer sets the property.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<!-- The configuration file of the project no longer sets the sealed property: it keeps the value of the parent directory. -->
</Project>
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.PS0207_Fixed;
[PSerializable]
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry( MethodExecutionArgs args )
{
Console.WriteLine( $"Entering {args.Method.Name}." );
}
}
public class OrderService
{
[Log]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Order placed for {product}." );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
Entering PlaceOrder.
Order placed for Coffee.
See Also
Other Resources
Configuration File Schema Reference
Configuring Projects Using postsharp.config