Open sandboxFocusImprove this doc

LA0143: Initialization Advice Has an Invalid Signature

Severity Error
Reported by PostSharp
Message Method "<Method>" must have the signature "void Method(AspectInitializationReason)" because it is annotated with [InitializeAspectInstanceAdvice] or [OnAspectsInitializedAdvice].

Cause

The InitializeAspectInstanceAdvice and OnAspectsInitializedAdvice advices make PostSharp call a method of the aspect when the aspect instance is initialized, and pass it the reason of the initialization: a constructor, a call of MemberwiseClone, deserialization, or a manual initialization. PostSharp calls this method with a single argument of type AspectInitializationReason, so the method must take exactly one parameter of this type and return void.

PostSharp reports LA0143 on the advice method when it has no parameter, more than one parameter, a parameter of another type, or a return type other than void. <Method> is the advice method.

In the following example, the advice method of the [TrackInstances] aspect has no parameter.

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

namespace Errors.LA0143_Error;

[PSerializable]
public class TrackInstancesAttribute : InstanceLevelAspect
{
    // An initialization advice must take a single AspectInitializationReason parameter.
    [InitializeAspectInstanceAdvice]
    public void OnInstanceInitialized()
    {
        Console.WriteLine( $"Aspect initialized for a {this.Instance!.GetType().Name}." );
    }
}

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

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().Name );
    }
}
error LA0143: LA0143_Error.cs(13,17): Method "Errors.LA0143_Error.TrackInstancesAttribute.OnInstanceInitialized" must have the signature "void Method(AspectInitializationReason)" because it is annotated with [InitializeAspectInstanceAdvice] or [OnAspectsInitializedAdvice].

Resolution

Give the advice method the signature void Method(AspectInitializationReason): make it return void, and declare one parameter of type AspectInitializationReason, even if the method does not use it. The method must also be public, or PostSharp reports LA0087.

In the following example, the advice method receives the reason of the initialization and prints it.

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

namespace Errors.LA0143_Fixed;

[PSerializable]
public class TrackInstancesAttribute : InstanceLevelAspect
{
    // The advice receives the reason of the initialization.
    [InitializeAspectInstanceAdvice]
    public void OnInstanceInitialized( AspectInitializationReason reason )
    {
        Console.WriteLine( $"Aspect initialized for a {this.Instance!.GetType().Name} ({reason})." );
    }
}

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

public static class Program
{
    public static void Main()
    {
        Console.WriteLine( new Customer().Name );
    }
}
Aspect initialized for a Customer (Constructor).
Alice

See Also

Reference

InitializeAspectInstanceAdvice
OnAspectsInitializedAdvice
AspectInitializationReason

Other Resources

Initializing Aspects
Advice Methods