Open sandboxFocusImprove this doc

LA0283: Several Members of the Imported Type

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" is bound by [ImportMember] to a member of its type, but several members of "<TargetType>" match: <MemberNames>. Specify the name of the member.

Cause

An advice parameter annotated with ImportMemberAttribute without a member name receives the field or the property of the target type whose type matches the type of the parameter: a parameter passed by value accepts a member whose type can be assigned to it without a conversion other than boxing, and a parameter passed by reference, or an ILocationBinding<T>, requires exactly the type of the member. When several members match, PostSharp cannot choose one, and reports LA0283 on each target, whatever the value of IsRequired. <MemberNames> lists the matching members.

When the aspect requests fields with IntroduceFieldAdviceInstance, the requested fields of the matching type are the only candidates, so the error can also mean that the aspect requests several fields of the same type.

In the following example, the [LogBalance] aspect imports a decimal member of Account, which has two.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0283_Error;

[PSerializable]
public sealed class LogBalanceAttribute : MethodLevelAspect
{
    [OnMethodSuccessAdvice]
    public static void OnSuccess( [ImportMember] decimal balance, [DeclarationName] string methodName )
    {
        Console.WriteLine( "Balance after {0}: {1}", methodName, balance );
    }
}

public class Account
{
    private decimal balance;
    private decimal overdraftLimit = 100;

    public bool CanWithdraw( decimal amount ) => amount <= this.balance + this.overdraftLimit;

    // Account has two fields of type decimal, and the aspect does not say which one it imports.
    [LogBalance]
    public void Deposit( decimal amount ) => this.balance += amount;
}

public static class Program
{
    public static void Main()
    {
        Account account = new Account();
        account.Deposit( 50 );
        account.Deposit( 25 );
        Console.WriteLine( "Can withdraw 150: {0}", account.CanWithdraw( 150 ) );
    }
}
error LA0283: LA0283_Error.cs(27,17): The parameter "balance" of the advice "Errors.LA0283_Error.LogBalanceAttribute.OnSuccess(System.Decimal, System.String)" is bound by [ImportMember] to a member of its type, but several members of "Errors.LA0283_Error.Account" match: balance, overdraftLimit. Specify the name of the member.

Resolution

  • Specify the name of the member in the constructor of ImportMemberAttribute. When the targets name the member differently, give several names, in order of precedence.
  • Change the type of the advice parameter, or pass it by reference, so that only one member matches.

In the following example, the aspect imports the balance field by its name.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0283_Fixed;

[PSerializable]
public sealed class LogBalanceAttribute : MethodLevelAspect
{
    // The name selects the balance field among the decimal fields of the target type.
    [OnMethodSuccessAdvice]
    public static void OnSuccess( [ImportMember( "balance" )] decimal balance, [DeclarationName] string methodName )
    {
        Console.WriteLine( "Balance after {0}: {1}", methodName, balance );
    }
}

public class Account
{
    private decimal balance;
    private decimal overdraftLimit = 100;

    public bool CanWithdraw( decimal amount ) => amount <= this.balance + this.overdraftLimit;

    [LogBalance]
    public void Deposit( decimal amount ) => this.balance += amount;
}

public static class Program
{
    public static void Main()
    {
        Account account = new Account();
        account.Deposit( 50 );
        account.Deposit( 25 );
        Console.WriteLine( "Can withdraw 150: {0}", account.CanWithdraw( 150 ) );
    }
}
Balance after Deposit: 50
Balance after Deposit: 75
Can withdraw 150: True

See Also

Reference

ImportMemberAttribute

Other Resources

Advice Parameters