| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" is bound by [ImportMember] with IsRequired set to true, but the type "<TargetType>" has no accessible field or property that a parameter of type "<ParameterType>" can receive. |
Cause
An advice parameter annotated with ImportMemberAttribute without a member name receives the field or the property of the target type whose type matches the type of the parameter. The candidates are the fields and properties of the target type and the accessible ones of its base types, except indexers, constants and the fields generated by the compiler. A member matches when:
- the parameter is passed by value, and the type of the member can be assigned to
<ParameterType>without a conversion other than boxing; - the parameter is passed by reference, with
in,reforout, or is an ILocationBinding<T>, and the member has exactly its type. Areforoutparameter also requires a field that is not read-only.
When no member matches, the parameter normally receives null, the default value, or a reference to a temporary variable. When IsRequired is true, PostSharp reports LA0260 on each target instead. <TargetType> is the type to which the aspect is applied, or the type that declares the target member. When several members match, PostSharp reports LA0283. When the advice uses a pointcut that skips the targets whose signature does not match, such as MatchPointcut, PostSharp skips the target instead of reporting the error.
In the following example, the [LogSession] aspect requires a member of type Guid, but OrderService has none.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0260_Error;
[PSerializable]
public sealed class LogSessionAttribute : MethodLevelAspect
{
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember( IsRequired = true )] Guid sessionId, [DeclarationName] string methodName )
{
Console.WriteLine( "Session {0}: {1}", sessionId, methodName );
}
}
public class OrderService
{
private readonly string customer = "Alice";
// OrderService has no field or property of type Guid.
[LogSession]
public void PlaceOrder() => Console.WriteLine( "Order placed for {0}.", this.customer );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
error LA0260: LA0260_Error.cs(24,17): The parameter "sessionId" of the advice "Errors.LA0260_Error.LogSessionAttribute.OnEntry(System.Guid, System.String)" is bound by [ImportMember] with IsRequired set to true, but the type "Errors.LA0260_Error.OrderService" has no accessible field or property that a parameter of type "System.Guid" can receive.
Resolution
- Add a field or a property of the expected type to the target type.
- Change the type of the advice parameter to a type that a member of the target type can be assigned to, or pass the parameter by value to accept boxing.
- Set IsRequired to
falseif the advice can work with the default value. - Otherwise, apply the aspect only to the types that have such a member.
In the following example, OrderService has a SessionId property, which the aspect receives.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0260_Fixed;
[PSerializable]
public sealed class LogSessionAttribute : MethodLevelAspect
{
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember( IsRequired = true )] Guid sessionId, [DeclarationName] string methodName )
{
Console.WriteLine( "Session {0}: {1}", sessionId, methodName );
}
}
public class OrderService
{
private readonly string customer = "Alice";
// The aspect imports this property because it is the only member of type Guid.
public Guid SessionId { get; } = new Guid( "6f9619ff-8b86-d011-b42d-00c04fc964ff" );
[LogSession]
public void PlaceOrder() => Console.WriteLine( "Order placed for {0}.", this.customer );
}
public static class Program
{
public static void Main()
{
new OrderService().PlaceOrder();
}
}
Session 6f9619ff-8b86-d011-b42d-00c04fc964ff: PlaceOrder
Order placed for Alice.
See Also
Reference
ImportMemberAttribute
IsRequired
Other Resources