Open sandboxFocusImprove this doc

LA0075: Imported Method Not Found in the Target Type

Severity Error
Reported by PostSharp
Message Cannot bind field "<Field>" to type "<TargetType>": cannot find a method named "<MethodName>" with signature "<Signature>" in type "<TargetType>" or its ancestors, or the method is not accessible from type "<TargetType>".

Cause

An aspect imports a method of its target class with a field of a delegate type annotated with ImportMemberAttribute. PostSharp looks for a method with the name given to the attribute and with the signature of the Invoke method of the delegate, in the target type and in its base types. When the IsRequired property is true, PostSharp reports LA0075 for each target type where it finds no such method. When IsRequired is false, which is the default, the field stays null instead.

The method is not found in the following cases:

  • No method has this name, for example because of a typo.
  • The parameters or the return type of the method differ from those of the delegate. An in parameter of the delegate also requires an in parameter of the method, not a ref one.
  • The method exists, but it is not accessible from the target type, for example a private method of a base class.

<MethodName> lists the names given to the attribute, and <Signature> is the signature of the delegate. PostSharp reports the error on the target type, not on the field of the aspect.

In the following example, the [SaveOnCreation] aspect imports the Save method of Document into a field of type Action, but Save takes a string parameter.

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

namespace Errors.LA0075_Error;

[PSerializable]
public class SaveOnCreationAttribute : InstanceLevelAspect
{
    // Action has no parameter, but the Save method of Document takes a string.
    [ImportMember( "Save", IsRequired = true )]
    public Action? Save;

    public override void RuntimeInitializeInstance()
    {
        this.Save!();
    }
}

[SaveOnCreation]
public class Document
{
    public void Save( string comment ) => Console.WriteLine( $"Document saved: {comment}" );
}

public static class Program
{
    public static void Main()
    {
        _ = new Document();
    }
}
error LA0075: LA0075_Error.cs(22,14): Cannot bind field "Errors.LA0075_Error.SaveOnCreationAttribute.Save" to type "Errors.LA0075_Error.Document": cannot find a method named "Save" with signature "() : void" in type "Errors.LA0075_Error.Document" or its ancestors, or the method is not accessible from type "Errors.LA0075_Error.Document".

Resolution

  • Give the field a delegate type with the same parameters and return type as the method, for example Action<T> for a method that takes one parameter and returns void.
  • Correct the name given to ImportMemberAttribute, or pass several names when the method can have one of several names.
  • Make the method of the base class accessible to the target class, for example protected instead of private.
  • When the aspect can work without the method, set IsRequired to false and check that the field is not null before invoking it.

In the following example, the field has the type Action<string>, which matches the signature of Save.

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

namespace Errors.LA0075_Fixed;

[PSerializable]
public class SaveOnCreationAttribute : InstanceLevelAspect
{
    // The delegate type has the signature of the Save method of Document.
    [ImportMember( "Save", IsRequired = true )]
    public Action<string>? Save;

    public override void RuntimeInitializeInstance()
    {
        this.Save!( "created" );
    }
}

[SaveOnCreation]
public class Document
{
    public void Save( string comment ) => Console.WriteLine( $"Document saved: {comment}" );
}

public static class Program
{
    public static void Main()
    {
        _ = new Document();
    }
}
Document saved: created

See Also

Reference

ImportMemberAttribute
IsRequired

Other Resources

Accessing Members of the Target Class