I can't generate a readonly record struct using Roslyn.
My code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
var tree = SyntaxFactory.CompilationUnit()
.AddMembers(
SyntaxFactory.RecordDeclaration(
SyntaxFactory.Token(SyntaxKind.RecordKeyword),
SyntaxFactory.Identifier("A"))
.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken))
.AddModifiers(
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword))
.WithClassOrStructKeyword(
SyntaxFactory.Token(SyntaxKind.StructKeyword))
.WithParameterList(
SyntaxFactory.ParameterList()))
.NormalizeWhitespace();
Console.WriteLine(tree.ToFullString());
var refApis = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !a.IsDynamic)
.Select(a => MetadataReference.CreateFromFile(a.Location));
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
var compilation = CSharpCompilation.Create("something", new SyntaxTree[] { tree.SyntaxTree }, refApis, options);
var diag = compilation.GetDiagnostics().Where(e => e.Severity == DiagnosticSeverity.Error).ToList();
foreach (var d in diag)
{
Console.WriteLine(d);
}
Console.ReadLine();
I get such a result "public readonly record struct A();" and such an error "(1,31): error CS0106: The "readonly" modifier is invalid for this element."
CSharpSyntaxTree.ParseText("public readonly record struct A();")and this compiles without the error. Inspecting both syntax trees they look the same but there must be some minor difference not immediately visible that triggers the compile error.