| Severity | Error |
| Reported by | PostSharp |
| Message | Cannot call AspectUtilities.InitializeCurrentAspects from the static method "<Method>": this call initializes the aspects of the current instance, so it must be made from an instance method. |
Cause
The constructors of a class enhanced by aspects initialize the aspects of each new object. When an object is created without a constructor, for example by a custom deserializer that calls GetUninitializedObject(Type), you initialize its aspects yourself by calling InitializeCurrentAspects(). PostSharp replaces this call, at build time, with a call to the InitializeAspects method of the current object, this.
A static method has no current object, so PostSharp reports LA0042 when the call is in a static method, including a static factory method that creates the object. <Method> is the static method that contains the call. The message is reported on the line of the call.
In the following example, the static Restore method creates a Customer without its constructor, and then calls InitializeCurrentAspects() itself.
using System;
using System.Runtime.CompilerServices;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.LA0042_Error;
// An instance-level aspect, initialized for each object when the object is created.
[PSerializable]
public sealed class TrackedAttribute : InstanceLevelAspect
{
public override void RuntimeInitializeInstance()
{
Console.WriteLine( $"Tracking {this.Instance!.GetType().Name}." );
}
}
[Tracked]
public class Customer
{
public string? Name { get; set; }
public void Greet() => Console.WriteLine( $"Hello, {this.Name}." );
// Creates a customer without calling its constructor, as a custom deserializer does.
public static Customer Restore( string name )
{
var customer = (Customer) RuntimeHelpers.GetUninitializedObject( typeof(Customer) );
customer.Name = name;
// The call is in a static method, so PostSharp does not know which object to initialize.
AspectUtilities.InitializeCurrentAspects();
return customer;
}
}
public static class Program
{
public static void Main()
{
var customer = Customer.Restore( "Alice" );
customer.Greet();
}
}
error LA0042: LA0042_Error.cs(32,9): Cannot call AspectUtilities.InitializeCurrentAspects from the static method "Errors.LA0042_Error.Customer.Restore(System.String)": this call initializes the aspects of the current instance, so it must be made from an instance method.
Resolution
Declare a protected virtual void InitializeAspects() method in the class, as described in Coping with Custom Object Serializers, and call it on the new object from the static method. PostSharp adds the initialization of the aspects of the class to this method, after your own code.
In the following example, Restore calls InitializeAspects on the new Customer, which initializes its [Tracked] aspect.
using System;
using System.Runtime.CompilerServices;
using PostSharp.Aspects;
using PostSharp.Serialization;
namespace Errors.LA0042_Fixed;
// An instance-level aspect, initialized for each object when the object is created.
[PSerializable]
public sealed class TrackedAttribute : InstanceLevelAspect
{
public override void RuntimeInitializeInstance()
{
Console.WriteLine( $"Tracking {this.Instance!.GetType().Name}." );
}
}
[Tracked]
public class Customer
{
public string? Name { get; set; }
public void Greet() => Console.WriteLine( $"Hello, {this.Name}." );
// Creates a customer without calling its constructor, as a custom deserializer does.
public static Customer Restore( string name )
{
var customer = (Customer) RuntimeHelpers.GetUninitializedObject( typeof(Customer) );
customer.Name = name;
// Initializes the aspects of the new object, which the constructor would otherwise do.
customer.InitializeAspects();
return customer;
}
// PostSharp adds the initialization of the aspects of Customer to this method.
protected virtual void InitializeAspects()
{
}
}
public static class Program
{
public static void Main()
{
var customer = Customer.Restore( "Alice" );
customer.Greet();
}
}
Tracking Customer.
Hello, Alice.
See Also
Reference
AspectUtilities
InstanceLevelAspect
Other Resources