| Severity | Warning |
| Reported by | Metalama.Framework |
| Message | The aspect '<Aspect>' uses features of C# <RequiredCSharpVersion>, but it is used in a project built with C# <TargetCSharpVersion>. Consider specifying <LangVersion><RequiredCSharpVersion></LangVersion> in this project or removing newer language features from the template '<Template>' and then specifying <MetalamaTemplateLanguageVersion> in the aspect project. |
Cause
When Metalama compiles a template, it records the highest C# version that the syntax of the template requires, including the syntax of its compile-time parts. When the template is expanded in a project whose C# version, given by the LangVersion MSBuild property, is lower than this version, the generated code may not compile.
Metalama reports LAMA0282 when an aspect defined in one project, typically an aspect library, is applied in another project that uses an older version of C#. The placeholders of the message are the aspect, the C# version required by the template, the C# version of the current project, and the template. Metalama reports the warning once per aspect class, on a declaration to which the aspect is applied.
In the following example, the template of the Log aspect contains a raw string literal, which requires C# 11, and the aspect is applied in a project that uses C# 10. The raw string literal ends up in the generated code, so the C# compiler additionally reports CS8936, and Metalama reports LAMA0232, because the template language version is also C# 10 in this example.
1namespace Doc.LAMA0282.Error;
2
3public class Calculator
4{
5 // The [Log] aspect uses C# 11 features.
6 [Log]
Warning LAMA0282: The aspect 'Log' uses features of C# 11.0, but it is used in a project built with C# 10.0. Consider specifying <LangVersion>11.0</LangVersion> in this project or removing newer language features from the template 'LogAttribute.OverrideMethod()' and then specifying <MetalamaTemplateLanguageVersion> in the aspect project.
7 public int Add( int a, int b ) => a + b;
8}
9
1using System;
2
3namespace Doc.LAMA0282.Error;
4
5public class Calculator
6{
7 // The [Log] aspect uses C# 11 features.
8 [Log]
9 public int Add(int a, int b)
10 {
Error CS8936: Feature 'raw string literals' is not available in C# 10.0. Please use language version 11.0 or greater.
11 var message = """Logged by the "Log" aspect.""";
12 Console.WriteLine(message);
13 return a + b;
14 }
15}
16
using Metalama.Framework.Aspects;
using System;
namespace Doc.LAMA0282.Error;
public class LogAttribute : OverrideMethodAspect
{
public override dynamic? OverrideMethod()
{
// Raw string literals require C# 11, but the target project uses C# 10.
var message = """Logged by the "Log" aspect.""";
Console.WriteLine( message );
return meta.Proceed();
}
}Resolution
Depending on your role, do one of the following:
- If you use the aspect, raise the
LangVersionproperty of your project to at least the version given in the message. - If you author the aspect, remove the newer language features from the template, as in the following example where the raw string literal is replaced by a regular string literal. To make sure that your templates stay compatible with older C# versions, set the
MetalamaTemplateLanguageVersionproperty of the aspect project to the lowest C# version you want to support. Metalama then reports LAMA0232 on any template syntax that requires a later version.
1namespace Doc.LAMA0282.Fixed;
2
3public class Calculator
4{
5 // The [Log] aspect now uses only C# 10 features.
6 [Log]
7 public int Add( int a, int b ) => a + b;
8}
9
1using System;
2
3namespace Doc.LAMA0282.Fixed;
4
5public class Calculator
6{
7 // The [Log] aspect now uses only C# 10 features.
8 [Log]
9 public int Add(int a, int b)
10 {
11 var message = "Logged by the \"Log\" aspect.";
12 Console.WriteLine(message);
13 return a + b;
14 }
15}
16
using Metalama.Framework.Aspects;
using System;
namespace Doc.LAMA0282.Fixed;
public class LogAttribute : OverrideMethodAspect
{
public override dynamic? OverrideMethod()
{
// A regular string literal is compatible with C# 10.
var message = "Logged by the \"Log\" aspect.";
Console.WriteLine( message );
return meta.Proceed();
}
}