| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot serialize a FieldInfo into an aspect, because a field can be transformed into a property by the weaver. Use a LocationInfo instead. |
Cause
PostSharp serializes the aspects at build time, after their CompileTimeInitialize method, and deserializes them at run time. A field of the aspect can hold a reflection object, such as a Type or a MethodInfo, but not a FieldInfo: when an aspect such as a LocationInterceptionAspect is applied to a field, the weaver turns the field into a property, so the field no longer exists at run time.
PostSharp reports LA0111 when it serializes a FieldInfo in an aspect, whether the aspect uses PSerializableAttribute or binary serialization. The error is reported on the assembly, without the location of the aspect, and the message does not name the aspect or the field: look for the fields of type FieldInfo, or of arrays and collections of it, in your aspects.
The following aspect stores the fields of its target type in a FieldInfo[].
using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.LA0111_Error;
[PSerializable]
public class ListFieldsAttribute : TypeLevelAspect
{
// The aspect stores FieldInfo objects, which PostSharp refuses to serialize.
private FieldInfo[]? fields;
public override void CompileTimeInitialize( Type type, AspectInfo aspectInfo )
{
this.fields = type.GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
}
public override void RuntimeInitialize( Type type )
{
foreach ( var field in this.fields! )
{
Console.WriteLine( $"{type.Name} has the field {field.Name}." );
}
}
}
[ListFields]
public class Customer
{
private string name = "Alice";
public string Name => this.name;
}
public static class Program
{
public static void Main()
{
Console.WriteLine( new Customer().Name );
}
}
error LA0111: Cannot serialize a FieldInfo into an aspect because a field can be transformed into a property by the weaver. Use a LocationInfo instead.
Resolution
Store a LocationInfo instead of the FieldInfo. A LocationInfo represents a field, a property or a parameter, and remains valid when the weaver turns a field into a property. Create it with ToLocationInfo or ToLocationInfoArray.
In the following example, the aspect stores a LocationInfo[].
using System;
using System.Reflection;
using PostSharp.Aspects;
using PostSharp.Reflection;
using PostSharp.Serialization;
namespace Errors.LA0111_Fixed;
[PSerializable]
public class ListFieldsAttribute : TypeLevelAspect
{
// The aspect stores LocationInfo objects, which remain valid when a field becomes a property.
private LocationInfo[]? fields;
public override void CompileTimeInitialize( Type type, AspectInfo aspectInfo )
{
this.fields = LocationInfo.ToLocationInfoArray( type.GetFields( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ) );
}
public override void RuntimeInitialize( Type type )
{
foreach ( var field in this.fields! )
{
Console.WriteLine( $"{type.Name} has the field {field.Name}." );
}
}
}
[ListFields]
public class Customer
{
private string name = "Alice";
public string Name => this.name;
}
public static class Program
{
public static void Main()
{
Console.WriteLine( new Customer().Name );
}
}
Customer has the field name.
Alice
See Also
Reference
LocationInfo
PSerializableAttribute
Other Resources