| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot apply the aspect "<Aspect>" to the instance constructor of type "<Type>": a method interception aspect cannot be applied to an instance constructor. Restrict the aspect to methods, for example with the AttributeTargetElements property. |
Cause
A MethodInterceptionAspect replaces the body of its target method with a call to OnInvoke, which decides whether to proceed to the original method. PostSharp does not support this transformation on constructors: it reports LA0113 instead of weaving the aspect into an instance constructor.
The usual cause is multicasting: when you apply the aspect to a class, PostSharp applies it to the members that the aspect accepts, and MethodInterceptionAspect accepts both methods and instance constructors. PostSharp also reports LA0113 when an IAspectProvider provides the aspect to a constructor, or when an OnMethodInvokeAdvice targets a constructor.
<Type> is the type that declares the constructor. When the aspect is multicast from a class, the message points to the class.
In the following example, the [Log] aspect is applied to the OrderService class, so it is also applied to its constructor.
using System;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.LA0113_Error;
[PSerializable]
public class LogAttribute : MethodInterceptionAspect
{
public override void OnInvoke( MethodInterceptionArgs args )
{
Console.WriteLine( $"Calling {args.Method.Name}." );
args.Proceed();
}
}
// The aspect is multicast to all methods of the class, including its constructor.
[Log]
public class OrderService
{
private readonly string _warehouse;
public OrderService( string warehouse )
{
this._warehouse = warehouse;
}
public void PlaceOrder( string product ) => Console.WriteLine( $"Order for {product} placed in {this._warehouse}." );
}
public static class Program
{
public static void Main()
{
new OrderService( "Brussels" ).PlaceOrder( "Chair" );
}
}
error LA0113: LA0113_Error.cs(19,14): Cannot apply the aspect "Errors.LA0113_Error.LogAttribute" to the instance constructor of type "Errors.LA0113_Error.OrderService": a method interception aspect cannot be applied to an instance constructor. Restrict the aspect to methods, for example with the AttributeTargetElements property.
Resolution
Restrict the aspect to methods:
- Set the AttributeTargetElements property of the aspect to Method where you apply it to a class.
- When an IAspectProvider provides the aspect, skip the constructors in its
ProvideAspectsmethod.
In the following example, the aspect applies to the PlaceOrder method only.
using System;
using PostSharp.Aspects;
using PostSharp.Extensibility;
using PostSharp.Serialization;
namespace Errors.LA0113_Fixed;
[PSerializable]
public class LogAttribute : MethodInterceptionAspect
{
public override void OnInvoke( MethodInterceptionArgs args )
{
Console.WriteLine( $"Calling {args.Method.Name}." );
args.Proceed();
}
}
// The aspect is multicast to the methods of the class only.
[Log( AttributeTargetElements = MulticastTargets.Method )]
public class OrderService
{
private readonly string _warehouse;
public OrderService( string warehouse )
{
this._warehouse = warehouse;
}
public void PlaceOrder( string product ) => Console.WriteLine( $"Order for {product} placed in {this._warehouse}." );
}
public static class Program
{
public static void Main()
{
new OrderService( "Brussels" ).PlaceOrder( "Chair" );
}
}
Calling PlaceOrder.
Order for Chair placed in Brussels.
See Also
Reference
MethodInterceptionAspect
AttributeTargetElements
MulticastTargets
Other Resources
Intercepting Methods
Adding Aspects to Multiple Declarations Using Attributes