| Severity | Error |
| Reported by | PostSharp |
| Message | The aspect "<Aspect>" applied to "<Target>" attempts to introduce a field with an empty or white-space name with IntroduceFieldAdviceInstance. Set the Name property to null to identify the field by its type. |
Cause
An aspect attempts to introduce a field by returning an IntroduceFieldAdviceInstance from ProvideAdvices. Its Name property is either a name, which finds an existing field of this name or names the new field, or null, which attempts to introduce a field by its type. PostSharp reports LA0270 when the name is an empty string or contains only white space, because no field can have this name.
<Target> is the declaration to which the aspect is applied, where the error is located.
In the following example, the [CountCalls] aspect passes its CounterName property to the request, and this property is an empty string when the user does not set it.
using System;
using System.Collections.Generic;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0270_Error;
// Counts the calls of the target method for each object.
[PSerializable]
public sealed class CountCallsAttribute : MethodLevelAspect, IAdviceProvider
{
// The name of the counter field, which the aspect leaves empty when the user does not set it.
public string CounterName { get; set; } = "";
public IEnumerable<AdviceInstance> ProvideAdvices( object targetElement )
{
yield return new IntroduceFieldAdviceInstance( typeof(int) ) { Name = this.CounterName };
}
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember] ref int calls )
{
calls++;
Console.WriteLine( $"Call {calls}." );
}
}
public class OrderService
{
[CountCalls]
public void PlaceOrder()
{
}
}
public static class Program
{
public static void Main()
{
OrderService service = new OrderService();
service.PlaceOrder();
service.PlaceOrder();
}
}
error LA0270: LA0270_Error.cs(32,17): The aspect "Errors.LA0270_Error.CountCallsAttribute" applied to "Errors.LA0270_Error.OrderService.PlaceOrder" attempts to introduce a field with an empty or white-space name with IntroduceFieldAdviceInstance. Set the Name property to null to identify the field by its type.
Resolution
Set the Name property to null to identify the field by its type, or to a valid field name. When the name comes from a property of the aspect, make null its default value, or convert an empty value to null.
In the following example, the CounterName property is null by default, and the advice imports the counter by its type.
using System;
using System.Collections.Generic;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.LA0270_Fixed;
// Counts the calls of the target method for each object.
[PSerializable]
public sealed class CountCallsAttribute : MethodLevelAspect, IAdviceProvider
{
// The name of the counter field, or null for a field requested by its type.
public string? CounterName { get; set; }
public IEnumerable<AdviceInstance> ProvideAdvices( object targetElement )
{
yield return new IntroduceFieldAdviceInstance( typeof(int) ) { Name = this.CounterName };
}
[OnMethodEntryAdvice]
public static void OnEntry( [ImportMember] ref int calls )
{
calls++;
Console.WriteLine( $"Call {calls}." );
}
}
public class OrderService
{
[CountCalls]
public void PlaceOrder()
{
}
}
public static class Program
{
public static void Main()
{
OrderService service = new OrderService();
service.PlaceOrder();
service.PlaceOrder();
}
}
Call 1.
Call 2.
See Also
Reference
IntroduceFieldAdviceInstance
ImportMemberAttribute
Other Resources