Open sandboxFocusImprove this doc

THR043: AppliesToMethodOnly Argument Has No Effect on a Property

Severity Warning
Reported by PostSharp.Patterns.Threading
Message The appliesToMethodOnly argument of the [ExplicitlySynchronized] attribute applied to property "<Property>" has no effect: <Reason>. Removing the argument does not change the behavior.

Cause

On a method, the appliesToMethodOnly argument of ExplicitlySynchronizedAttribute chooses between two behaviors: with true, the threading model adds no code to the method; with false, the default, the method runs with the verification of field accesses disabled. See AppliesToMethodOnly.

On a property, the threading models ignore the argument, and the behavior depends only on the kind of property:

  • The accessors of an automatic property are not enhanced, as with true.
  • The accessors of a property that is not automatic run with verification disabled, as with false.

When the threading model of a class analyzes its properties, it reports THR043 for each property whose attribute receives the argument that requests the other behavior: true on a property that is not automatic, or false on an automatic property. <Reason> states the behavior that the property gets. Only an attribute applied to the property itself is reported, not one that reaches it by multicasting from the class.

In the following example, the ItemCount property is not automatic, but its attribute receives true.

using System;
using PostSharp.Patterns.Threading;

namespace Errors.THR043_Error;

[ReaderWriterSynchronized]
public class ShoppingCart
{
    private int itemCount;

    // On a property, the threading model ignores the appliesToMethodOnly argument.
    [ExplicitlySynchronized( true )]
    public int ItemCount
    {
        get => this.itemCount;
        set => this.itemCount = value;
    }
}

public static class Program
{
    public static void Main()
    {
        var cart = new ShoppingCart();
        cart.ItemCount = 3;
        Console.WriteLine( $"Items: {cart.ItemCount}" );
    }
}
warning THR043: THR043_Error.cs(13,16): The appliesToMethodOnly argument of the [ExplicitlySynchronized] attribute applied to property "Errors.THR043_Error.ShoppingCart.ItemCount" has no effect: the accessors of a property that is not automatic run with verification disabled. Removing the argument does not change the behavior.

Resolution

  • Remove the argument: the behavior of the property does not change.
  • When the accessors of a property that is not automatic must not be enhanced at all, replace the property with methods, where the argument applies.

In the following example, the argument is removed, and the accessors still run with verification disabled.

using System;
using PostSharp.Patterns.Threading;

namespace Errors.THR043_Fixed;

[ReaderWriterSynchronized]
public class ShoppingCart
{
    private int itemCount;

    // Without the argument, the accessors run with verification disabled, as before.
    [ExplicitlySynchronized]
    public int ItemCount
    {
        get => this.itemCount;
        set => this.itemCount = value;
    }
}

public static class Program
{
    public static void Main()
    {
        var cart = new ShoppingCart();
        cart.ItemCount = 3;
        Console.WriteLine( $"Items: {cart.ItemCount}" );
    }
}
Items: 3

See Also

Reference

ExplicitlySynchronizedAttribute
ReaderWriterSynchronizedAttribute

Other Resources

Opting In and Out From Thread Safety