| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot apply an instance advice of the instance-scoped aspect "<Aspect>" to the static member "<Member>" (make the advice static). |
Cause
An instance-scoped aspect, such as an InstanceLevelAspect applied to a class, has one instance for each instance of its target class. An advice method that is not static runs on this aspect instance. A static member, such as a static property, is not related to any instance of the class, so no aspect instance exists to run the advice.
When the pointcut of an instance advice method selects a static member, PostSharp reports LA0107 on that member. This often happens with a MulticastPointcut that sets only Targets, which selects the static members as well as the instance ones.
In the following example, the pointcut of the [TrackChanges] aspect selects all the properties of Customer, including the static Count property.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0107_Error;
[PSerializable]
public class TrackChangesAttribute : InstanceLevelAspect
{
[OnLocationSetValueAdvice, MulticastPointcut( Targets = MulticastTargets.Property )]
public void OnPropertySet( LocationInterceptionArgs args )
{
Console.WriteLine( $"Setting {args.LocationName} to {args.Value}." );
args.ProceedSetValue();
}
}
[TrackChanges]
public class Customer
{
// The pointcut selects this static property, but the advice is an instance method of an instance-level aspect.
public static int Count { get; set; }
public string? Name { get; set; }
}
public static class Program
{
public static void Main()
{
Customer.Count = 1;
new Customer().Name = "Alice";
}
}
error LA0107: LA0107_Error.cs(24,23): Cannot apply an instance advice of the instance-scoped aspect "Errors.LA0107_Error.TrackChangesAttribute" to the static member "Errors.LA0107_Error.Customer.Count" (make the advice static).
Resolution
- Exclude the static members from the pointcut, for instance with
Attributes = MulticastAttributes.Instancein the MulticastPointcut, or by filtering them out in the method of a MethodPointcut. - Or make the advice method static, as the message suggests, when it does not use the state of the aspect instance. PostSharp does not report LA0107 for a static advice method.
In the following example, the pointcut selects only the instance properties, so the advice does not apply to Count.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0107_Fixed;
[PSerializable]
public class TrackChangesAttribute : InstanceLevelAspect
{
// The pointcut selects only the instance properties.
[OnLocationSetValueAdvice, MulticastPointcut( Targets = MulticastTargets.Property, Attributes = MulticastAttributes.Instance )]
public void OnPropertySet( LocationInterceptionArgs args )
{
Console.WriteLine( $"Setting {args.LocationName} to {args.Value}." );
args.ProceedSetValue();
}
}
[TrackChanges]
public class Customer
{
public static int Count { get; set; }
public string? Name { get; set; }
}
public static class Program
{
public static void Main()
{
Customer.Count = 1;
new Customer().Name = "Alice";
}
}
Setting Name to Alice.
See Also
Reference
InstanceLevelAspect
MulticastPointcut
MulticastAttributes
Other Resources