| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | '<Type>' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but the automatic property '<Property>' has a setter that is not private, so not every assignment to it can be seen. Replace 'set' with 'init', make the setter private, or, for a positional record struct, declare it as a 'readonly record struct'. |
Cause
An aspect is instantiated once and reused for every declaration it applies to, and at design time it is reused across compilations. State that you keep in an aspect instance therefore leaks from one target to the next, in an order that you don't control. That's why Metalama requires aspects, fabrics, and validators to be written in an immutable style. The requirement comes from the [ImmutableType] attribute, which is applied to IAspect and Fabric, and which every type that derives from or implements a marked type inherits.
For such a type, every automatic property must have no setter, an init accessor, or a private setter that's assigned only in a constructor or in an init accessor. A property whose setter is public, internal, or protected can be assigned by code that Metalama doesn't see, so the declaration itself is reported.
Metalama reports LAMA0881 on the declaration of an instance automatic property whose set accessor is neither private nor init. Template properties and introduced properties aren't reported, because they're code that the aspect adds to its target, not state of the aspect. In the message, <Type> is the aspect, fabric, validator, or other type subject to the contract, and <Property> is the name of the property.
A positional record struct, such as record struct Range( int Start, int End ), is a special case. The compiler generates a settable property for each parameter of its primary constructor, so the type violates the contract although it looks like a record class. Metalama then reports the warning on the parameter, and the fix is to declare the type as a readonly record struct.
Fields are reported with LAMA0880.
In the following example, the Log aspect exposes its category as a property with a public setter, so that users can set it as a named argument of the attribute.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0881.Error;
5
6public class LogAttribute : OverrideMethodAspect
7{
8 // Warning: the setter is public, so code outside the aspect can change the property.
Warning LAMA0881: 'LogAttribute' must be written in an immutable style because it is an aspect, a fabric, a validator, or another type bound by the [ImmutableType] contract, but the automatic property 'Category' has a setter that is not private, so not every assignment to it can be seen. Replace 'set' with 'init', make the setter private, or, for a positional record struct, declare it as a 'readonly record struct'.
9 public string Category { get; set; } = "General";
10
11 public override dynamic? OverrideMethod()
12 {
13 Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
14
15 return meta.Proceed();
16 }
17}
18
1namespace Doc.LAMA0881.Error;
2
3internal class OrderService
4{
5 [Log( Category = "Orders" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0881.Error;
4
5internal class OrderService
6{
7 [Log(Category = "Orders")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15
Resolution
Choose one of the following options:
- Replace
setwithinit. The property can still be set in an object initializer and as a named argument of the aspect attribute, but not afterward. This is the preferred option for a property that users must be able to configure, shown in the following example. - Remove the setter, and assign the property from the constructor.
- Make the setter private, and make sure that the property is assigned only in a constructor.
- For a positional
record struct, add thereadonlymodifier to make it areadonly record struct. - Move the state out of the aspect. Keep per-target state in the objects that your template or
BuildAspectmethod creates, and not in a property of the aspect.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0881.Fixed;
5
6public class LogAttribute : OverrideMethodAspect
7{
8 // Fixed: the init accessor accepts the named argument but no later assignment.
9 public string Category { get; init; } = "General";
10
11 public override dynamic? OverrideMethod()
12 {
13 Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
14
15 return meta.Proceed();
16 }
17}
18
1namespace Doc.LAMA0881.Fixed;
2
3internal class OrderService
4{
5 [Log( Category = "Orders" )]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0881.Fixed;
4
5internal class OrderService
6{
7 [Log(Category = "Orders")]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("[Orders] Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15