Open sandboxFocusImprove this doc

LAMA0880: A field of an immutable type must be read-only or private

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 field '<Field>' is neither read-only nor private, so not every assignment to it can be seen. Add the 'readonly' modifier, or make the field private.

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 instance field must be read-only, or private and assigned only in a constructor or in an init accessor. A private field that isn't read-only is accepted at its declaration, because private write access confines every assignment to the declaring type, and Metalama then checks the assignments themselves. A field that is public, internal, or protected can be written by code that Metalama doesn't see, so the declaration itself is reported.

Metalama reports LAMA0880 on the declaration of an instance field that isn't const, readonly, or private. Template fields and introduced fields 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 <Field> is the name of the field. Automatic properties are reported with LAMA0881.

In the following example, the Log aspect stores its category in a public field that isn't read-only.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0880.Error;
5
6public class LogAttribute : OverrideMethodAspect
7{
8    // Warning: the field is public and not read-only, so code outside the aspect can change it.
    Warning LAMA0880: '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 field 'Category' is neither read-only nor private, so not every assignment to it can be seen. Add the 'readonly' modifier, or make the field private.

9    public string Category;
10
11    public LogAttribute( string category )
12    {
13        this.Category = category;
14    }
15
16    public override dynamic? OverrideMethod()
17    {
18        Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
19
20        return meta.Proceed();
21    }
22}
23
Source Code
1namespace Doc.LAMA0880.Error;
2


3internal class OrderService
4{
5    [Log( "Orders" )]
6    public void PlaceOrder( string product, int quantity )
7    {
8        // Places the order.
9    }


10}
11
Transformed Code
1using System;
2
3namespace Doc.LAMA0880.Error;
4
5internal class OrderService
6{
7    [Log("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:

  • Add the readonly modifier to the field and assign it in the constructor or in a field initializer. This is the preferred option, shown in the following example.
  • Make the field private, and make sure that it's assigned only in a constructor.
  • Replace the field with an automatic property that has an init accessor. Use this option when the value is set as a named argument of the aspect attribute, because a read-only field can't be a named argument.
  • Move the state out of the aspect. Keep per-target state in the objects that your template or BuildAspect method creates, and not in a field of the aspect.

If the type of the field isn't immutable either, Metalama reports it separately once the field is read-only.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0880.Fixed;
5
6public class LogAttribute : OverrideMethodAspect
7{
8    // Fixed: the field is read-only, so only the constructor can assign it.
9    public readonly string Category;
10
11    public LogAttribute( string category )
12    {
13        this.Category = category;
14    }
15
16    public override dynamic? OverrideMethod()
17    {
18        Console.WriteLine( $"[{this.Category}] Entering {meta.Target.Method}." );
19
20        return meta.Proceed();
21    }
22}
23
Source Code
1namespace Doc.LAMA0880.Fixed;
2


3internal class OrderService
4{
5    [Log( "Orders" )]
6    public void PlaceOrder( string product, int quantity )
7    {
8        // Places the order.
9    }


10}
11
Transformed Code
1using System;
2
3namespace Doc.LAMA0880.Fixed;
4
5internal class OrderService
6{
7    [Log("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

See also