| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | '<Type>' is a run-time-or-compile-time type, but the static member '<Member>' <Cause>. A static field of a type that is loaded at compile time is shared by every aspect instance, every target and every compilation, and at design time it keeps its value for as long as the analysis process runs. Mark the member with [RunTime] so that it exists only at run time, or make it read-only and of an immutable type. |
Cause
A run-time-or-compile-time type exists both in your program and in the compile-time assembly that Metalama loads into the compiler and IDE processes. Aspects are run-time-or-compile-time types, and so are the types marked with [RunTimeOrCompileTime]. At compile time, a static field of such a type is shared by every aspect instance, every target, and every compilation that the process handles, just like a static field of a compile-time type (see LAMA0889).
Metalama reports LAMA0891 on a static field, static automatic property, or static field-like event of a run-time-or-compile-time type when the member isn't marked [RunTime] and either:
- isn't read-only: the field isn't
readonlyorconst, the property has a setter or aninitaccessor, or the member is an event, or - is read-only but of a type that isn't immutable, for instance
List<T>,Dictionary<TKey, TValue>, or an array.
<Cause> says which of the two applies: is not read-only, or is of type '<MemberType>', which is not immutable (path: <Path>), where the path explains why the type isn't immutable. The rules for immutable types are those of LAMA0890. Template members and introduced members aren't reported, and neither is a member whose own type is run-time-only.
In the following example, the Log aspect keeps the prefix of its messages in a writable static field of the aspect class. Because the aspect class is loaded at compile time, the field is shared by every compilation.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0891.Error;
5
6public class LogAttribute : OverrideMethodAspect
7{
8 // Warning: the field also exists at compile time, where it is shared by all compilations.
Warning LAMA0891: 'LogAttribute' is a run-time-or-compile-time type, but the static member 'Prefix' is not read-only. A static field of a compile-time type is shared by every aspect instance, every target and every compilation, and at design time it keeps its value for as long as the analysis process runs. Mark the member with [RunTime] so that it exists only at run time, or make it read-only and of an immutable type.
9 public static string Prefix = "Entering";
10
11 public override dynamic? OverrideMethod()
12 {
13 Console.WriteLine( $"{Prefix} {meta.Target.Method}." );
14
15 return meta.Proceed();
16 }
17}
18
1namespace Doc.LAMA0891.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.LAMA0891.Error;
4
5internal class OrderService
6{
7 [Log]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15
Resolution
Choose one of the following options:
- If the value never changes, make the member read-only and of an immutable type: add the
readonlymodifier or remove the setter, and replace a mutable type such asList<T>withImmutableArray<T>. The following example uses this fix. - If only run-time code uses the member, mark it with [RunTime]. Metalama then removes it from the compile-time assembly, so it can't be shared between compilations. Compile-time code, such as
BuildAspect, can no longer access it. The initializer of the member is still validated as code that can run at compile time, so it can't reference run-time-only declarations such asConsole(LAMA0236). Assign such a value from run-time code instead. - If the member holds state that varies between targets, don't keep it in a static member. Use the properties of the aspect or aspect options instead.
The [RunTime] option exists only for run-time-or-compile-time types. In a compile-time type, a run-time member is an error (LAMA0292), which is why LAMA0889 and LAMA0890 don't suggest it.
1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0891.Fixed;
5
6public class LogAttribute : OverrideMethodAspect
7{
8 // Fixed: the field is read-only and of an immutable type.
9 public static readonly string Prefix = "Entering";
10
11 public override dynamic? OverrideMethod()
12 {
13 Console.WriteLine( $"{Prefix} {meta.Target.Method}." );
14
15 return meta.Proceed();
16 }
17}
18
1namespace Doc.LAMA0891.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.LAMA0891.Fixed;
4
5internal class OrderService
6{
7 [Log]
8 public void PlaceOrder(string product, int quantity)
9 {
10 Console.WriteLine("Entering OrderService.PlaceOrder(string, int).");
11
12 // Places the order.
13 }
14}
15