| Severity | Error |
| Reported by | PostSharp |
| Message | The method "<Method>" cannot be static, because PostSharp uses it to initialize the aspects of each instance. |
Cause
To initialize the instance-scoped aspects of a class, PostSharp generates two methods without parameters in this class, InitializeAspects and OnInstanceConstructed, and calls them on each new instance. OnInstanceConstructed exists when an aspect has an OnInstanceConstructedAdvice. When the class already defines a method with one of these names and signatures, PostSharp uses it instead of generating one. It reports PS0118 when this method <Method> is static, because it must run on an instance.
When the class is public and not sealed, PostSharp reports PS0117 instead, because a static method is not virtual either. PS0118 therefore concerns internal or sealed classes.
In the following example, the internal class Invoice has an aspect with an OnInstanceConstructedAdvice, and defines a static OnInstanceConstructed method.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.PS0118_Error;
// Reports the creation of each instance of the target class.
[PSerializable]
public class TrackCreationAttribute : InstanceLevelAspect
{
[OnInstanceConstructedAdvice]
public void OnInstanceConstructed() => Console.WriteLine( $"{this.Instance!.GetType().Name} created." );
}
[TrackCreation]
internal class Invoice
{
public static int Count { get; private set; }
// PostSharp calls this method on each new instance, so it cannot be static.
protected static void OnInstanceConstructed()
{
Count++;
Console.WriteLine( "Invoice ready." );
}
}
public static class Program
{
public static void Main()
{
_ = new Invoice();
Console.WriteLine( $"Invoices: {Invoice.Count}" );
}
}
error PS0118: PS0118_Error.cs(22,27): The method "Errors.PS0118_Error.Invoice.OnInstanceConstructed" cannot be static, because PostSharp uses it to initialize the aspects of each instance.
Resolution
- Make the method an instance method, preferably
protected virtual, so that derived classes can override it. PostSharp then adds its code to your method and calls it when an instance is constructed. - Otherwise, rename the method, so that PostSharp generates its own one.
In the following example, OnInstanceConstructed is an instance method. PostSharp calls it after the constructor, then runs the advice of the aspect.
using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;
namespace Errors.PS0118_Fixed;
// Reports the creation of each instance of the target class.
[PSerializable]
public class TrackCreationAttribute : InstanceLevelAspect
{
[OnInstanceConstructedAdvice]
public void OnInstanceConstructed() => Console.WriteLine( $"{this.Instance!.GetType().Name} created." );
}
[TrackCreation]
internal class Invoice
{
public static int Count { get; private set; }
// An instance method: PostSharp calls it on each new instance.
protected virtual void OnInstanceConstructed()
{
Count++;
Console.WriteLine( "Invoice ready." );
}
}
public static class Program
{
public static void Main()
{
_ = new Invoice();
Console.WriteLine( $"Invoices: {Invoice.Count}" );
}
}
Invoice ready.
Invoice created.
Invoices: 1
See Also
Reference
OnInstanceConstructedAdvice
InstanceLevelAspect
Other Resources