| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" is bound by <Binding>, which a method annotated with [OnInstanceConstructedAdvice] does not support. |
Cause
An advice method annotated with OnInstanceConstructedAdvice runs once an object of the target type is constructed, whichever constructor ran. It runs in a method without parameters, and its target is the type, not a method. It therefore has no argument, no return value, no field or property value and no control flow to change. When a parameter of this advice is bound by ArgumentAttribute, ReturnValueAttribute, LocationValueAttribute or FlowBehaviorAttribute, PostSharp reports LA0224 on the parameter.
<Binding> is the binding attribute as you wrote it, such as [Argument("name")]. The other binding attributes that this advice does not accept report LA0151.
In the following example, the instance constructed advice of the [TraceCreation] aspect tries to receive the name argument of the constructor of Customer.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0224_Error;
[PSerializable]
public sealed class TraceCreationAttribute : InstanceLevelAspect
{
// The advice runs after any constructor, so it has no argument to receive.
[OnInstanceConstructedAdvice]
public void OnInstanceConstructed( [Argument( "name" )] string name )
{
Console.WriteLine( $"Created {name}" );
}
}
[TraceCreation]
public class Customer
{
public Customer( string name )
{
this.Name = name;
}
public string Name { get; }
public override string ToString() => $"Customer {this.Name}";
}
public static class Program
{
public static void Main()
{
_ = new Customer( "Alice" );
}
}
error LA0224: LA0224_Error.cs(13,68): The parameter "name" of the advice "Errors.LA0224_Error.TraceCreationAttribute.OnInstanceConstructed(System.String)" is bound by [Argument("name")], which a method annotated with [OnInstanceConstructedAdvice] does not support.
Resolution
- Receive the constructed object with a ThisAttribute parameter, and read the values that the constructor stored in it.
- To receive the arguments of a constructor, apply a method-level aspect to the constructor instead, with an OnMethodEntryAdvice or OnMethodSuccessAdvice that binds them with ArgumentAttribute.
In the following example, the [TraceCreation] aspect receives the constructed Customer.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0224_Fixed;
[PSerializable]
public sealed class TraceCreationAttribute : InstanceLevelAspect
{
// The advice receives the constructed object, which holds the values passed to the constructor.
[OnInstanceConstructedAdvice]
public void OnInstanceConstructed( [This] object instance )
{
Console.WriteLine( $"Created {instance}" );
}
}
[TraceCreation]
public class Customer
{
public Customer( string name )
{
this.Name = name;
}
public string Name { get; }
public override string ToString() => $"Customer {this.Name}";
}
public static class Program
{
public static void Main()
{
_ = new Customer( "Alice" );
}
}
Created Customer Alice
See Also
Reference
OnInstanceConstructedAdvice
ArgumentAttribute
ThisAttribute
Other Resources