| 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 '<Member>' is assigned here, outside a constructor. Move the assignment into a constructor or an 'init' accessor. |
Cause
Aspects, fabrics and validators must be written in an immutable style, because Metalama can share one instance among several threads and several compilations. A type is subject to this rule when it carries the [ImmutableType] attribute or when it derives from, or implements, a type that requires immutability, such as IAspect or Fabric.
A field or an automatic property whose write access is private is accepted by the declaration rules, because every assignment to it is in the declaring type, where Metalama can see it. Metalama instead checks the assignments themselves, and reports LAMA0887 on each one that happens outside a constructor or an init accessor of the declaring type. In the message, <Type> is the type that must be immutable and <Member> is the name of the field or property that is written.
The rule applies to the following write operations on an instance member:
- simple, compound and null-coalescing assignments, such as
this._count = 1,this._count += 1orthis._cache ??= value; - increments and decrements, such as
this._count++; - deconstructions, where each target member is reported separately;
- assignments in a lambda expression or a local function, even when it is written inside a constructor, because the delegate can run at any later time;
- assignments to a member of another instance of the same type, even in a constructor.
Metalama doesn't report object initializers and with expressions, static and constant members, members that are advice (for example members marked with the [Template] or [Introduce] attributes, which are injected into the target code and aren't state of the aspect), or ref arguments. An out argument is reported by LAMA0888.
In the following example, the Log aspect stores the name of its target method in a private field during BuildAspect, so that the template can read it.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0887.Error;
6
7public class LogAttribute : OverrideMethodAspect
8{
9 private string? _methodName;
10
11 public override void BuildAspect( IAspectBuilder<IMethod> builder )
12 {
13 // Warning: the aspect field is assigned outside a constructor.
Warning LAMA0887: '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 '_methodName' is assigned here, outside a constructor. Move the assignment into a constructor or an 'init' accessor.
14 this._methodName = builder.Target.Name;
15
16 base.BuildAspect( builder );
17 }
18
19 public override dynamic? OverrideMethod()
20 {
21 Console.WriteLine( $"Entering {this._methodName}." );
22
23 return meta.Proceed();
24 }
25}
26
1namespace Doc.LAMA0887.Error;
2
3internal class OrderService
4{
5 [Log]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0887.Error;
4
5internal class OrderService
6{
7 [Log]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering PlaceOrder.");
11
12 // Places the order.
13 }
14}
15
Resolution
- If the value comes from the target declaration, don't store it in the aspect: read it from meta.Target in the template, or pass it to the template as an argument.
- Compute the value in the constructor and assign it there, or initialize the field with an initializer.
- Use an
initaccessor for a property that callers set when they create the object. - If the value changes over time, don't keep it in the aspect. Store it in an object that is created for each use, or in a local variable.
In the following example, the template reads the method name from meta.Target, and the aspect no longer has a field.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0887.Fixed;
5
6public class LogAttribute : OverrideMethodAspect
7{
8 public override dynamic? OverrideMethod()
9 {
10 // Fixed: the template reads the method name from meta.Target instead of an aspect field.
11 Console.WriteLine( $"Entering {meta.Target.Method.Name}." );
12
13 return meta.Proceed();
14 }
15}
16
1namespace Doc.LAMA0887.Fixed;
2
3internal class OrderService
4{
5 [Log]
6 public void PlaceOrder( string product, int quantity )
7 {
8 // Places the order.
9 }
10}
11
1using System;
2
3namespace Doc.LAMA0887.Fixed;
4
5internal class OrderService
6{
7 [Log]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering PlaceOrder.");
11
12 // Places the order.
13 }
14}
15