| Severity | Error |
| Reported by | Metalama.Framework |
| Message | Cannot serialize the compile-time array of type '<Type>' to a run-time value because it has more than one dimension. Use a jagged array instead. |
Cause
When a template uses a compile-time array in a run-time context, Metalama generates an array creation expression that contains the converted items. This conversion only supports one-dimensional arrays. Arrays of arrays, also known as jagged arrays, are one-dimensional, so they're supported.
Metalama reports LAMA0202 when it expands the template and finds an array with more than one dimension, for instance an int[,], whether the array is stored in a variable of a statically typed array type or in an object variable. The error is reported on the target declaration of the aspect. In the message, <Type> is the CLR name of the array type, for instance Int32[,].
In the following example, the aspect introduces a method that returns a multiplication table computed at compile time by a compile-time helper class, as a two-dimensional array of type int[,].
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0202.Error;
4
5public class MultiplicationTableAttribute : TypeAspect
6{
7 // Introduces a method that returns a lookup table computed at compile time.
8 [Introduce]
9 public object GetMultiplicationTable()
10 {
11 var table = TableBuilder.Build( 3 );
12
13 // Error: a multidimensional array can't be converted to a run-time value.
14 return meta.RunTime( table );
15 }
16}
17
18[CompileTime]
19public static class TableBuilder
20{
21 // Builds a two-dimensional multiplication table.
22 public static int[,] Build( int size )
23 {
24 var table = new int[size, size];
25
26 for ( var i = 0; i < size; i++ )
27 {
28 for ( var j = 0; j < size; j++ )
29 {
30 table[i, j] = (i + 1) * (j + 1);
31 }
32 }
33
34 return table;
35 }
36}
37
1namespace Doc.LAMA0202.Error;
2
3[MultiplicationTable]
Error LAMA0202: Cannot serialize the compile-time array of type 'Int32[,]' to a run-time value because it has more than one dimension. Use a jagged array instead.
4public partial class Calculator
5{
6}
7
Resolution
Use a jagged array, for instance int[][] instead of int[,], for the compile-time value. In the following example, the helper class builds a jagged array. If the run-time code needs a multidimensional array, convert the jagged array at run time.
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0202.Fixed;
4
5public class MultiplicationTableAttribute : TypeAspect
6{
7 // Introduces a method that returns a lookup table computed at compile time.
8 [Introduce]
9 public object GetMultiplicationTable()
10 {
11 var table = TableBuilder.Build( 3 );
12
13 // Fixed: a jagged array can be converted to a run-time value.
14 return meta.RunTime( table );
15 }
16}
17
18[CompileTime]
19public static class TableBuilder
20{
21 // Fixed: builds a jagged array instead of a two-dimensional array.
22 public static int[][] Build( int size )
23 {
24 var table = new int[size][];
25
26 for ( var i = 0; i < size; i++ )
27 {
28 table[i] = new int[size];
29
30 for ( var j = 0; j < size; j++ )
31 {
32 table[i][j] = (i + 1) * (j + 1);
33 }
34 }
35
36 return table;
37 }
38}
39
1namespace Doc.LAMA0202.Fixed;
2
3[MultiplicationTable]
4public partial class Calculator
5{
6}
7
1namespace Doc.LAMA0202.Fixed;
2
3[MultiplicationTable]
4public partial class Calculator
5{
6 public object GetMultiplicationTable()
7 {
8 return new int[][] {
9 new int[] {
10 1,
11 2,
12 3
13 },
14 new int[] {
15 2,
16 4,
17 6
18 },
19 new int[] {
20 3,
21 6,
22 9
23 }
24 };
25 }
26}
27