Open sandboxFocusImprove this doc

LA0080: Imported Property Not Found in the Target Type

Severity Error
Reported by PostSharp
Message Cannot bind field "<Field>" to type "<TargetType>": cannot find a property named "<PropertyName>"<Index> in type "<TargetType>" or its ancestors.

Cause

An aspect imports a property of its target class with a field of type Property<TValue> or Property<TValue, TIndex> annotated with ImportMemberAttribute. PostSharp looks for a property with the name given to the attribute in the target type and in its base types; for a Property<TValue, TIndex> field, the property must be an indexer with one parameter of the TIndex type. When the IsRequired property is true, PostSharp reports LA0080 for each target type where it finds no such property. When IsRequired is false, which is the default, the field stays null instead.

The property is not found in the following cases:

  • No property has this name, for example because of a typo.
  • The property is a private property of a base class.
  • The field is a Property<TValue, TIndex>, but the property is not an indexer, or its parameter has another type.

<PropertyName> lists the names given to the attribute. <Index> describes the index parameter for a Property<TValue, TIndex> field, and is empty otherwise. PostSharp reports the error on the target type, not on the field of the aspect.

In the following example, the [TraceCreation] aspect imports a FullName property, but Customer only has a Name property.

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

namespace Errors.LA0080_Error;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    // Customer has no property named FullName.
    [ImportMember( "FullName", IsRequired = true )]
    public Property<string>? Name;

    public override void RuntimeInitializeInstance()
    {
        Console.WriteLine( $"Created {this.Name!.Get()}" );
    }
}

[TraceCreation]
public class Customer
{
    public string Name { get; set; } = "Alice";
}

public static class Program
{
    public static void Main()
    {
        _ = new Customer();
    }
}
error LA0080: LA0080_Error.cs(22,14): Cannot bind field "Errors.LA0080_Error.TraceCreationAttribute.Name" to type "Errors.LA0080_Error.Customer": cannot find a property named "FullName" in type "Errors.LA0080_Error.Customer" or its ancestors.

Resolution

  • Correct the name given to ImportMemberAttribute, or pass several names when the property can have one of several names.
  • Make the property of the base class accessible to the target class, for example protected instead of private.
  • When the aspect can work without the property, set IsRequired to false and check that the field is not null before using it.

In the following example, the attribute gives the name of the Name property.

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

namespace Errors.LA0080_Fixed;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    // The attribute gives the name of the property of Customer.
    [ImportMember( "Name", IsRequired = true )]
    public Property<string>? Name;

    public override void RuntimeInitializeInstance()
    {
        Console.WriteLine( $"Created {this.Name!.Get()}" );
    }
}

[TraceCreation]
public class Customer
{
    public string Name { get; set; } = "Alice";
}

public static class Program
{
    public static void Main()
    {
        _ = new Customer();
    }
}
Created Alice

See Also

Reference

ImportMemberAttribute
Property<TValue>
Property<TValue, TIndex>

Other Resources

Accessing Members of the Target Class