Open sandboxFocusImprove this doc

LA0087: Advice Member Not Public

Severity Error
Reported by PostSharp
Message Advice "<Advice>" is not valid on "<Member>" because this member is not public.

Cause

PostSharp requires the members of an aspect that carry an advice attribute to be public. It reports LA0087 when one of the following members is not public:

<Advice> is the type of the advice attribute, and <Member> is the member of the aspect that carries it.

In the following example, the OnEntry advice method is internal.

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

namespace Errors.LA0087_Error;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    // An advice method must be public.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    internal void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
error LA0087: LA0087_Error.cs(14,19): Advice "PostSharp.Aspects.Advices.OnMethodEntryAdvice" is not valid on "Errors.LA0087_Error.TraceMethodsAttribute.OnEntry(System.String)" because this member is not public.

Resolution

Make the member public. To control the visibility of an introduced member in the target class, keep the member of the aspect public and set the Visibility property of IntroduceMemberAttribute.

In the following example, the OnEntry advice method is public.

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

namespace Errors.LA0087_Fixed;

[PSerializable]
public class TraceMethodsAttribute : TypeLevelAspect
{
    // The advice method is public.
    [OnMethodEntryAdvice, MulticastPointcut( Targets = MulticastTargets.Method )]
    public void OnEntry( [DeclarationName] string methodName )
    {
        Console.WriteLine( $"Entering {methodName}." );
    }
}

[TraceMethods]
public class OrderService
{
    public void PlaceOrder( string product ) => Console.WriteLine( $"Order placed: {product}" );
}

public static class Program
{
    public static void Main()
    {
        new OrderService().PlaceOrder( "Book" );
    }
}
Entering PlaceOrder.
Order placed: Book

See Also

Reference

OnMethodEntryAdvice
ImportMemberAttribute
IntroduceMemberAttribute

Other Resources

Advice Methods
Accessing Members of the Target Class
Introducing Interfaces, Methods, Properties and Events into Existing Classes