Open sandboxFocusImprove this doc

LA0081: Imported Property Has Another Type Than the Field

Severity Error
Reported by PostSharp
Message Cannot bind field "<Field>" to property "<Property>": the type of the property differs from the TValue type argument of the field.

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 binds the Get and Set delegates of the field to the accessors of the property, and the TValue type argument of the field must be exactly the type of the property. A type to which the property is assignable, such as object for a string property, is not accepted.

PostSharp reports LA0081 when it finds the property but its type differs. <Property> is the name of the property, without its type. PostSharp reports the error on the target type, not on the field of the aspect.

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

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

namespace Errors.LA0081_Error;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    // The Name property of Customer is a string, not an object.
    [ImportMember( "Name", IsRequired = true )]
    public Property<object>? 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 LA0081: LA0081_Error.cs(22,14): Cannot bind field "Errors.LA0081_Error.TraceCreationAttribute.Name" to property "Name": the type of the property differs from the TValue type argument of the field.

Resolution

Give the field the type argument that is the type of the property. When the aspect needs the value as another type, convert it after reading it with Get.

In the following example, the field has the type Property<string>.

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

namespace Errors.LA0081_Fixed;

[PSerializable]
public class TraceCreationAttribute : InstanceLevelAspect
{
    // The type argument of the field is exactly the type of the property.
    [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>

Other Resources

Accessing Members of the Target Class