| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" is bound by [This] with IsRequired set to true, but "<Target>" is static. |
Cause
A parameter of an advice annotated with ThisAttribute receives the instance of the target, that is, the this object of the method, field or property. A static target has no instance, so the parameter normally receives null, or the default value of its type. When IsRequired is true, the advice states that it needs an instance, and PostSharp reports LA0218 for each static target of the advice instead of passing null.
The message is reported on the static target. It is often a member that the aspect reaches by multicasting, when the aspect is applied to a whole type.
In the following example, the [LogInstance] aspect is applied to the Order class, so it is multicast to all its methods, including the static Create method.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0218_Error;
[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Method )]
public class LogInstanceAttribute : MethodLevelAspect
{
[OnMethodEntryAdvice]
public static void OnEntry( [DeclarationName] string methodName, [This( IsRequired = true )] object instance )
{
Console.WriteLine( $"Entering {methodName} on an instance of {instance.GetType().Name}." );
}
}
[LogInstance]
public class Order
{
private readonly string number;
public Order( string number )
{
this.number = number;
}
public void Ship()
{
Console.WriteLine( $"Order {this.number} shipped." );
}
// The aspect is multicast to this static method, which has no instance.
public static Order Create( string number ) => new Order( number );
}
public static class Program
{
public static void Main()
{
Order.Create( "A-17" ).Ship();
}
}
error LA0218: LA0218_Error.cs(36,25): The parameter "instance" of the advice "Errors.LA0218_Error.LogInstanceAttribute.OnEntry(System.String, System.Object)" is bound by [This] with IsRequired set to true, but "Errors.LA0218_Error.Order.Create(System.String)" is static.
Resolution
- Exclude the static members from the targets of the aspect, for example with the AttributeTargetMemberAttributes property set to Instance.
- Remove the aspect from the static member, when you applied it explicitly.
- When the advice can work without an instance, set IsRequired to
false, which is the default, and handle anullinstance in the advice.
In the following example, the aspect is multicast to the instance methods of Order only.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0218_Fixed;
[PSerializable]
[MulticastAttributeUsage( MulticastTargets.Method )]
public class LogInstanceAttribute : MethodLevelAspect
{
[OnMethodEntryAdvice]
public static void OnEntry( [DeclarationName] string methodName, [This( IsRequired = true )] object instance )
{
Console.WriteLine( $"Entering {methodName} on an instance of {instance.GetType().Name}." );
}
}
// The aspect is multicast to the instance methods only.
[LogInstance( AttributeTargetMemberAttributes = MulticastAttributes.Instance )]
public class Order
{
private readonly string number;
public Order( string number )
{
this.number = number;
}
public void Ship()
{
Console.WriteLine( $"Order {this.number} shipped." );
}
public static Order Create( string number ) => new Order( number );
}
public static class Program
{
public static void Main()
{
Order.Create( "A-17" ).Ship();
}
}
Entering Ship on an instance of Order.
Order A-17 shipped.
See Also
Reference
ThisAttribute
AttributeTargetMemberAttributes
Other Resources