| Severity | Error |
| Reported by | PostSharp |
| Message | The parameter "<Parameter>" of the advice "<Advice>" is of type "<ParameterType>", which cannot receive the reflection object bound by [Declaration] in this kind of advice. The parameter must be of one of these types: <ValidTypes>. |
Cause
A [DeclarationAttribute] parameter receives the reflection object of the target of the advice. Its type depends on the kind of advice:
- In a method boundary or method interception advice, a MethodBase, which is a MethodInfo for a method and a ConstructorInfo for a constructor.
- In a location advice, a LocationInfo, or the PropertyInfo of the property. A FieldInfo is not accepted: PostSharp encapsulates an intercepted field into a property, so the target is a property at run time.
- In an OnInstanceConstructedAdvice, the Type of the instance.
PostSharp reports LA0246 when the type <ParameterType> of the parameter <Parameter> can receive none of these objects. <ValidTypes> lists the types accepted in the advice <Advice>; a base type of one of them, such as MemberInfo or object, is also accepted.
In the following example, the [LogChanges] aspect intercepts a field and asks for its FieldInfo.
using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0246_Error;
[PSerializable]
public class LogChangesAttribute : LocationLevelAspect
{
[OnLocationSetValueAdvice]
public static void OnSetValue(
LocationInterceptionArgs args,
// PostSharp encapsulates the field into a property, so the declaration is not a FieldInfo.
[Declaration] FieldInfo field )
{
args.ProceedSetValue();
Console.WriteLine( $"{field.Name} set to {args.Value}." );
}
}
public class Order
{
[LogChanges]
public string Status = "New";
}
public static class Program
{
public static void Main()
{
new Order().Status = "Shipped";
}
}
error LA0246: LA0246_Error.cs(16,33): The parameter "field" of the advice "Errors.LA0246_Error.LogChangesAttribute.OnSetValue(PostSharp.Aspects.LocationInterceptionArgs, System.Reflection.FieldInfo)" is of type "System.Reflection.FieldInfo", which cannot receive the reflection object bound by [Declaration] in this kind of advice. The parameter must be of one of these types: "PostSharp.Reflection.LocationInfo", "System.Reflection.PropertyInfo" or a base type.
Resolution
Change the type of the parameter to one of the types that the message lists, or to a base type. In a location advice, use LocationInfo, which represents a field or a property.
In the following example, the advice receives a LocationInfo.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Reflection;
using PostSharp.Serialization;
namespace Errors.LA0246_Fixed;
[PSerializable]
public class LogChangesAttribute : LocationLevelAspect
{
[OnLocationSetValueAdvice]
public static void OnSetValue(
LocationInterceptionArgs args,
// A LocationInfo represents the field or the property.
[Declaration] LocationInfo location )
{
args.ProceedSetValue();
Console.WriteLine( $"{location.Name} set to {args.Value}." );
}
}
public class Order
{
[LogChanges]
public string Status = "New";
}
public static class Program
{
public static void Main()
{
new Order().Status = "Shipped";
}
}
Status set to Shipped.
See Also
Reference
DeclarationAttribute
LocationInfo
Other Resources