| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" is of type "<ParameterType>", but a parameter bound by <Binding> must be of type "<RequiredType>". |
Cause
A parameter annotated with DeclarationNameAttribute receives the name of the target declaration, which is a constant string. Unlike the bindings that accept a base type of the bound value, such as object for ArgumentAttribute, this binding requires a parameter of exactly the type string, because a parameter of type object would only box the name. Any other type reports LA0229.
<Binding> is the binding attribute as written in source code, [DeclarationName], and <RequiredType> is string. PostSharp reports the message on the advice parameter. A ref, out or in parameter reports LA0213 instead.
In the following example, the OnEntry advice receives the name of the method in a parameter of type object.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0229_Error;
[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
// The name of the method is a string, but the parameter is of type object.
[OnMethodEntryAdvice]
public void OnEntry( [DeclarationName] object methodName )
{
Console.WriteLine( $"Entering {methodName}." );
}
}
public class OrderService
{
[Trace]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Ordered {product}." );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
error LA0229: LA0229_Error.cs(13,51): The parameter "methodName" of the advice "Errors.LA0229_Error.TraceAttribute.OnEntry(System.Object)" is of type "object", but a parameter bound by [DeclarationName] must be of type "string".
Resolution
Declare the parameter as string, passed by value.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0229_Fixed;
[PSerializable]
public class TraceAttribute : MethodLevelAspect
{
// The parameter is of type string, the type of the name.
[OnMethodEntryAdvice]
public void OnEntry( [DeclarationName] string methodName )
{
Console.WriteLine( $"Entering {methodName}." );
}
}
public class OrderService
{
[Trace]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Ordered {product}." );
}
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder( "Coffee" );
}
}
Entering PlaceOrder.
Ordered Coffee.
See Also
Reference
Other Resources