Open sandboxFocusImprove this doc

PS0253: Configuration Object Has the Wrong Type for Its Property

Severity Error
Reported by PostSharp
Message The type <ObjectType> cannot be converted to <MemberType>.

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. A child element named after a property or a field sets it to the object that its own child element describes. The name and the XML namespace of that inner element give the type of the object.

PostSharp reports PS0253 when this type, <ObjectType>, cannot be assigned to the type of the property or the field, <MemberType>. The location of the message is the element of the property in postsharp.config.

In the following example, the DefaultOptions of the Orders profile contain the options of the console backend instead of logging options. The options of a backend are run-time options, which a profile does not hold.

<?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">
        <!-- The options of the console backend are not options of a logging profile. -->
        <DefaultOptions>
          <ConsoleLoggingBackendOptions xmlns="clr-namespace:PostSharp.Patterns.Diagnostics.Backends.Console;assembly:PostSharp.Patterns.Diagnostics" />
        </DefaultOptions>
      </LoggingProfile>
    </Profiles>
  </Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.PS0253_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 PS0253: postsharp.config(7,10): The type PostSharp.Patterns.Diagnostics.Backends.Console.ConsoleLoggingBackendOptions cannot be converted to PostSharp.Patterns.Diagnostics.LoggingOptions.

Resolution

  • Use an element of the type of the property, or of a type derived from it. For the DefaultOptions and ExceptionOptions of a logging profile, this is a LoggingOptions element.
  • Check the XML namespace of the element: an xmlns attribute on the element, or on one of its parents, decides in which CLR namespace and assembly PostSharp looks for the type.
  • Set run-time options, such as the options of a logging backend, in the code of the application. See Formatting Log Records.

In the following example, the DefaultOptions of the profile contain a LoggingOptions element.

<?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">
        <!-- The default options of the profile are a LoggingOptions element. -->
        <DefaultOptions>
          <LoggingOptions Level="Info" />
        </DefaultOptions>
      </LoggingProfile>
    </Profiles>
  </Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.PS0253_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

LoggingProfile
LoggingOptions
ConsoleLoggingBackendOptions

Other Resources

Including CLR Objects in Configuration
Formatting Log Records
Configuration File Schema Reference