Open sandboxFocusImprove this doc

LA0265: Required Method Not Found for IMethodBinding

Severity Error
Reported by PostSharp
Message The parameter "<Parameter>" of the advice "<Advice>" is bound by [ImportMember] with IsRequired set to true, but the type "<TargetType>" has no accessible method named "<MemberName>" that is not generic and does not return by reference.

Cause

A parameter of type IMethodBinding annotated with ImportMemberAttribute receives an object that calls a method of the target type, selected by its name. The candidates are the methods of the target type and the accessible methods of its base types. PostSharp excludes the generic methods, whose type arguments an IMethodBinding cannot supply, and the methods that return by reference.

When no method remains, the parameter normally receives null. When IsRequired is true, PostSharp reports LA0265 instead, for each target of the advice. A field or a property of the name does not count: IMethodBinding imports methods only.

The message is reported on the target of the advice, and <MemberName> lists the names given to the attribute.

In the following example, the [LogState] aspect imports a DescribeState method, but the Order class defines DescribeState as a property.

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

namespace Errors.LA0265_Error;

[PSerializable]
public class LogStateAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry( [This] object instance, [DeclarationName] string methodName,
                                [ImportMember( "DescribeState", IsRequired = true )] IMethodBinding describeState )
    {
        Console.WriteLine( $"{methodName}: {describeState.Invoke( ref instance, Arguments.Empty )}" );
    }
}

public class Order
{
    private string status = "New";

    // DescribeState is a property, but the aspect imports a method of this name.
    public string DescribeState => $"Order is {this.status}.";

    [LogState]
    public void Ship()
    {
        this.status = "Shipped";
        Console.WriteLine( "Order shipped." );
    }
}

public static class Program
{
    public static void Main()
    {
        new Order().Ship();
    }
}
error LA0265: LA0265_Error.cs(27,17): The parameter "describeState" of the advice "Errors.LA0265_Error.LogStateAttribute.OnEntry(System.Object, System.String, PostSharp.Aspects.IMethodBinding)" is bound by [ImportMember] with IsRequired set to true, but the type "Errors.LA0265_Error.Order" has no accessible method named "DescribeState" that is not generic and does not return by reference.

Resolution

  • Define a method of the expected name in the target type, or correct the name given to the attribute.
  • To import a field or a property, use a parameter of the type of the member or of type ILocationBinding<T> instead of IMethodBinding.
  • When the advice can work without the method, set IsRequired to false, which is the default, and handle a null binding.

In the following example, DescribeState is a method, which the aspect calls before Ship changes the status.

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

namespace Errors.LA0265_Fixed;

[PSerializable]
public class LogStateAttribute : MethodLevelAspect
{
    [OnMethodEntryAdvice]
    public static void OnEntry( [This] object instance, [DeclarationName] string methodName,
                                [ImportMember( "DescribeState", IsRequired = true )] IMethodBinding describeState )
    {
        Console.WriteLine( $"{methodName}: {describeState.Invoke( ref instance, Arguments.Empty )}" );
    }
}

public class Order
{
    private string status = "New";

    // DescribeState is a method, which the aspect can import.
    public string DescribeState() => $"Order is {this.status}.";

    [LogState]
    public void Ship()
    {
        this.status = "Shipped";
        Console.WriteLine( "Order shipped." );
    }
}

public static class Program
{
    public static void Main()
    {
        new Order().Ship();
    }
}
Ship: Order is New.
Order shipped.

See Also

Reference

ImportMemberAttribute
IMethodBinding

Other Resources

Advice Parameters