Source generators should target .NET Standard 2.0. This is the last .NET Standard version that supports .NET Framework. The default C# version in latest .NET Framework releases is C# 7.3.
Suppose a source generator should be consumed by .NET Framework users that are using C# 7.3 in their consuming project. Does the generated source need to be C# 7.3 or would it be possible to push it further and avoid language features that would need newer versions of .NET?
Specifically I am interested in following C# 8 and above features:
ref readonly
parameters(in parameters would theoretically work) (C# 12)Since source generators output source into the consuming project/assembly, the generated source would be compiled with C# 7.3 features. Is there any way to walk around that? Can we compile specific files with other C# version?. Or can source generators output to entirely new assembly and somehow inform the compiler about the dependency(this would probably not work regardless because of partial types which must be fully defined inside an assembly)?
Edit 1: I am using Incremental Source Generators, not ISourceGenerator.
The code generator implementation itself must target .NET Standard 2.0 but can use any C# version you want. I understand that your question pertains to the C# version of the generated code, which be lower or equal to the version of the current C# project.
Your code generator implementation can read the current C# version using the following code:
public static LanguageVersion GetLanguageVersion( this Compilation compilation )
{
var tree = compilation.SyntaxTrees.FirstOrDefault();
if ( tree == null )
{
return LanguageVersionFacts.MapSpecifiedToEffectiveVersion( LanguageVersion.Default );
}
return ((CSharpParseOptions) tree.Options).LanguageVersion;
}
Your implementation can then generate the code dynamically as needed by the current C# version.