Open sandboxFocusImprove this doc

LAMA0294: A C# feature isn't supported in compile-time code

Severity Error
Reported by Metalama.Framework
Message The declaration '<Declaration>' cannot use the C# feature '<Feature>', because compile-time code is compiled for netstandard2.0, which does not support this feature.

Cause

Metalama compiles compile-time code, such as aspects, fabrics, and types marked with [CompileTime] or [RunTimeOrCompileTime], into a separate assembly that targets .NET Standard 2.0, whatever the target framework of your project. Some C# features require support from the runtime and can't be used in this assembly.

Metalama reports LAMA0294 when a compile-time or run-time-or-compile-time type uses such a feature. Currently, the only feature checked is inline arrays: a struct marked with the [InlineArray] attribute, which requires .NET 8 or later. The first placeholder of the message is the feature, and the second is the declaration that uses it.

This diagnostic replaces LAMA0281, which earlier versions of Metalama reported in the same situation.

In the following example, the Log aspect uses a compile-time inline array.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4using System.Runtime.CompilerServices;
5
6namespace Doc.LAMA0294.Error;
7
8// Error: compile-time code can't declare an inline array.
9[CompileTime]
10[InlineArray( 4 )]
Error LAMA0294: The declaration 'ParameterIndexes' cannot use the C# feature 'inline arrays', because compile-time code is compiled for netstandard2.0, which does not support this feature.

11public struct ParameterIndexes
12{
13    private int _element0;
14}
15
16public class LogAttribute : OverrideMethodAspect
17{
18    public override void BuildAspect( IAspectBuilder<IMethod> builder )
19    {
20        base.BuildAspect( builder );
21
22        var indexes = new ParameterIndexes();
23
24        for ( var i = 0; i < 4; i++ )
25        {
26            indexes[i] = i;
27        }
28    }
29
30    public override dynamic? OverrideMethod()
31    {
32        Console.WriteLine( $"Executing {meta.Target.Method}." );
33
34        return meta.Proceed();
35    }
36}
37
1namespace Doc.LAMA0294.Error;
2
3internal class Calculator
4{
5    [Log]
6    public int Add( int a, int b ) => a + b;
7}
8

Resolution

Do one of the following:

  • In compile-time code, use a regular array or another collection type instead of an inline array, as in the following example.
  • If only run-time code uses the type, remove the [CompileTime] or [RunTimeOrCompileTime] attribute so that the type stays in run-time code, where inline arrays are supported when the project targets .NET 8 or later. Templates can still use the type in run-time expressions.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0294.Fixed;
6
7public class LogAttribute : OverrideMethodAspect
8{
9    public override void BuildAspect( IAspectBuilder<IMethod> builder )
10    {
11        base.BuildAspect( builder );
12
13        // Fixed: compile-time code uses a regular array instead of an inline array.
14        var indexes = new int[4];
15
16        for ( var i = 0; i < 4; i++ )
17        {
18            indexes[i] = i;
19        }
20    }
21
22    public override dynamic? OverrideMethod()
23    {
24        Console.WriteLine( $"Executing {meta.Target.Method}." );
25
26        return meta.Proceed();
27    }
28}
29
Source Code
1namespace Doc.LAMA0294.Fixed;
2


3internal class Calculator
4{
5    [Log]
6    public int Add( int a, int b ) => a + b;
7}
8
Transformed Code
1using System;
2
3namespace Doc.LAMA0294.Fixed;
4
5internal class Calculator
6{
7    [Log]
8    public int Add(int a, int b)
9    {
10        Console.WriteLine("Executing Calculator.Add(int, int).");
11        return a + b;
12    }
13}
14

See also