Open sandboxFocusImprove this doc

LA0139: Field Imported into a Property Field

Severity Error
Reported by PostSharp
Message Cannot import "<Member>" into the aspect field "<AspectField>" because it is not a property. Only properties can be imported into a field of type Property<TValue> or Property<TValue,TIndex>.

Cause

An aspect imports a property of its target class into a field of type Property<TValue>, or an indexer into a field of type Property<TValue, TIndex>, with ImportMemberAttribute or with an ImportLocationAdviceInstance. At run time, the field holds delegates that call the accessors of the property, so PostSharp can only bind it to a property.

PostSharp looks up the member by name among the fields, properties and events of the target class, or takes the field or property that the ImportLocationAdviceInstance designates. It reports LA0139 when the member that it finds, <Member>, is a field or an event. The message is reported on the aspect field, <AspectField>.

In the following example, the [TraceCreation] aspect imports Name into a Property<string> field, but Name is a field of Customer.

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

namespace Errors.LA0139_Error;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    [ImportMember( "Name", IsRequired = true )]
    public Property<string>? Name;

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

[TraceCreation]
public class Customer
{
    // Name is a field, but a Property<string> field of the aspect can only import a property.
    public string Name = "Alice";
}

public static class Program
{
    public static void Main()
    {
        _ = new Customer();
    }
}
error LA0139: LA0139_Error.cs(12,30): Cannot import "Errors.LA0139_Error.Customer.Name" into the aspect field "Errors.LA0139_Error.TraceCreationAttribute.Name" because it is not a property. Only properties can be imported into a field of type Property<TValue> or Property<TValue,TIndex>.

Resolution

In the following example, Name is a property of Customer.

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

namespace Errors.LA0139_Fixed;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    [ImportMember( "Name", IsRequired = true )]
    public Property<string>? Name;

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

[TraceCreation]
public class Customer
{
    // Name is now a property, which the Property<string> field of the aspect can import.
    public string Name { get; set; } = "Alice";
}

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

See Also

Reference

ImportMemberAttribute
Property<TValue>
ImportLocationAdviceInstance
ILocationBinding

Other Resources

Accessing Members of the Target Class
Providing Advices Dynamically