Open sandboxFocusImprove this doc

LA0088: Advice Requires a Target Type

Severity Error
Reported by PostSharp
Message Cannot use the advice "<Advice>" in an aspect applied to <DeclarationKind> "<Declaration>" because this advice requires a target type. Use a type-level aspect instead, which you can multicast from the assembly to types.

Cause

Some advices transform the type that the aspect is applied to, or the declaring type of the member that it is applied to: IntroduceMemberAttribute, IntroduceInterfaceAttribute, ImportMemberAttribute, InitializeAspectInstanceAdvice, OnAspectsInitializedAdvice and OnInstanceConstructedAdvice. An aspect applied to the assembly itself, such as an AssemblyLevelAspect, has no such type, so PostSharp reports LA0088 for each of these advices.

<Advice> is the kind of advice, such as OnInstanceConstructed, not the name of the advice method. <DeclarationKind> and <Declaration> are the kind and the name of the target of the aspect, such as Assembly and the name of the assembly. The message has no location in the source code.

In the following example, the [CountInstances] aspect is an AssemblyLevelAspect with an OnInstanceConstructedAdvice.

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

[assembly: Errors.LA0088_Error.CountInstancesAttribute]

namespace Errors.LA0088_Error;

// The aspect is applied to the assembly, which has no instances to construct.
[PSerializable]
public class CountInstancesAttribute : AssemblyLevelAspect
{
    public static int Count;

    [OnInstanceConstructedAdvice]
    public void OnInstanceConstructed()
    {
        Count++;
    }
}

public class Order
{
}

public static class Program
{
    public static void Main()
    {
        _ = new Order();
        _ = new Order();

        Console.WriteLine( $"{CountInstancesAttribute.Count} instances constructed." );
    }
}
error LA0088: Cannot use the advice "OnInstanceConstructed" in an aspect applied to Assembly "LA0088_Error" because this advice requires a target type. Use a type-level aspect instead, which you can multicast from the assembly to types.

Resolution

Derive the aspect from a type-level aspect, such as InstanceLevelAspect or TypeLevelAspect, and apply it to the types that it must transform. To keep the custom attribute on the assembly, select the types with the AttributeTargetTypes property: PostSharp multicasts the aspect from the assembly to each of these types.

In the following example, the aspect is an InstanceLevelAspect multicast from the assembly to the Order class, and the advice runs each time an Order is constructed.

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

[assembly: Errors.LA0088_Fixed.CountInstancesAttribute( AttributeTargetTypes = "Errors.LA0088_Fixed.Order" )]

namespace Errors.LA0088_Fixed;

// A type-level aspect, multicast from the assembly to the selected types, has a target type for the advice.
[PSerializable]
public class CountInstancesAttribute : InstanceLevelAspect
{
    public static int Count;

    [OnInstanceConstructedAdvice]
    public void OnInstanceConstructed()
    {
        Count++;
    }
}

public class Order
{
}

public static class Program
{
    public static void Main()
    {
        _ = new Order();
        _ = new Order();

        Console.WriteLine( $"{CountInstancesAttribute.Count} instances constructed." );
    }
}
2 instances constructed.

See Also

Reference

AssemblyLevelAspect
InstanceLevelAspect
OnInstanceConstructedAdvice
AttributeTargetTypes

Other Resources

Developing Type- and Assembly-Level Aspects
Advice Methods
Adding Aspects to Multiple Declarations Using Attributes