| Severity | Error |
| Reported by | PostSharp |
| Message | The aspect "<Aspect>" applied to "<Target>" attempts to introduce a field <Name>of type "<FieldType>" in "<TargetType>", but the member "<Member>" has this name and is not a field of this type and kind. Choose another name, or set the WhenIncompatibleMemberExists property of the IntroduceFieldAdviceInstance to IntroduceAnother to introduce a field whose name is the requested name followed by a number. |
Cause
An aspect attempts to introduce a field in its target type by returning an IntroduceFieldAdviceInstance with a Name. PostSharp looks for a member of this name in the target type, then in the accessible members of its base types. LA0272 is reported when the first member that has the name cannot hold the requested value:
- a property, an event or a method;
- a field of another type, or a static field when the request is for an instance field, or the reverse;
- a constant, which has no storage.
The request can neither use this member nor introduce a field of the same name when its WhenIncompatibleMemberExists property is Fail, the default value. This property applies only to a request that has a name: a request without a name finds fields by their type. LA0272 is always an error.
<Name> is the requested name in quotes followed by a space. <Target> is the declaration to which the aspect is applied, where the error is located, and <Member> is the member that has the name.
In the following example, the [CountCalls] aspect attempts to introduce an int field named CallCount, but CallCount is a property of OrderService.
using System;
using System.Collections.Generic;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0272_Error;
[PSerializable]
public sealed class CountCallsAttribute : MethodLevelAspect, IAdviceProvider
{
public IEnumerable<AdviceInstance> ProvideAdvices( object targetElement )
{
yield return new IntroduceFieldAdviceInstance( typeof(int) ) { Name = "CallCount", WhenCompatibleFieldExists = IntroduceFieldCompatibleAction.Ignore };
}
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember] ref int callCount )
{
callCount++;
}
}
public class OrderService
{
private int callCount;
public int CallCount => this.callCount;
public void ResetCount() => this.callCount = 0;
// The aspect requests a CallCount field, but CallCount is a property of OrderService.
[CountCalls]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Placing an order for {product}." );
}
}
public static class Program
{
public static void Main()
{
var service = new OrderService();
service.PlaceOrder( "a book" );
service.PlaceOrder( "a pen" );
Console.WriteLine( $"{service.CallCount} orders placed." );
}
}
error LA0272: LA0272_Error.cs(34,17): The aspect "Errors.LA0272_Error.CountCallsAttribute" applied to "Errors.LA0272_Error.OrderService.PlaceOrder(System.String)" attempts to introduce a field "CallCount" of type "int32" in "Errors.LA0272_Error.OrderService", but the member "Errors.LA0272_Error.OrderService.CallCount" has this name and is not a field of this type and kind. Choose another name, or set the WhenIncompatibleMemberExists property of the IntroduceFieldAdviceInstance to IntroduceAnother to introduce a field whose name is the requested name followed by a number.
Resolution
- To use an existing field, request it by its own name, with WhenCompatibleFieldExists set to Ignore.
- To give the aspect a field of its own, choose a name that no member has, or set WhenIncompatibleMemberExists to IntroduceAnother. With this action, the new field gets the requested name followed by a number, such as
CallCount2, and the advices of the aspect still import it by the requested name.
In the following example, the aspect requests the callCount field, which is behind the CallCount property.
using System;
using System.Collections.Generic;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0272_Fixed;
[PSerializable]
public sealed class CountCallsAttribute : MethodLevelAspect, IAdviceProvider
{
public IEnumerable<AdviceInstance> ProvideAdvices( object targetElement )
{
// The request names the field behind the CallCount property.
yield return new IntroduceFieldAdviceInstance( typeof(int) ) { Name = "callCount", WhenCompatibleFieldExists = IntroduceFieldCompatibleAction.Ignore };
}
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember] ref int callCount )
{
callCount++;
}
}
public class OrderService
{
private int callCount;
public int CallCount => this.callCount;
public void ResetCount() => this.callCount = 0;
[CountCalls]
public void PlaceOrder( string product )
{
Console.WriteLine( $"Placing an order for {product}." );
}
}
public static class Program
{
public static void Main()
{
var service = new OrderService();
service.PlaceOrder( "a book" );
service.PlaceOrder( "a pen" );
Console.WriteLine( $"{service.CallCount} orders placed." );
}
}
Placing an order for a book.
Placing an order for a pen.
2 orders placed.
See Also
Reference
IntroduceFieldAdviceInstance
IntroduceFieldIncompatibleAction
Other Resources