Online demos: support C# 12+ syntax and preserve more trimmed types

Two follow-up fixes for compiling demos in Edit Source / Playground:

- CompilerService: raise the parse language version from C# 10 to Latest
  so demos using collection expressions and primary constructors compile
  (previously failed with CS8936).
- linker.xml: preserve System.Math, DisplayAttribute and Radzen.Colors,
  which are referenced only by demo source compiled at runtime and were
  otherwise removed by trimming (Math.PI, [Display(Description=...)],
  @Colors.* respectively).

Verified against a trimmed publish: demo compile failures drop from 20 to 5
out of 1052 (remaining 5 are a separate Razor type-inference codegen issue).
This commit is contained in:
Vladimir Enchev
2026-06-24 13:52:31 +03:00
parent 51ffb64225
commit 36e764d07b
2 changed files with 11 additions and 1 deletions

View File

@@ -196,7 +196,7 @@ namespace RadzenBlazorDemos
.Replace("#nullable enable", "#nullable disable")
.Replace("where TItem : , notnull", "where TItem : notnull");
var syntaxTree = CSharpSyntaxTree.ParseText(generatedCode, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp10));
var syntaxTree = CSharpSyntaxTree.ParseText(generatedCode, CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Latest));
compilation = compilation.RemoveAllSyntaxTrees().AddSyntaxTrees(syntaxTree);

View File

@@ -8,5 +8,15 @@
</assembly>
<!-- The runtime Razor/Roslyn compiler behind Edit Source / Playground relies on the [EventHandler] metadata (e.g. oninput -> ChangeEventArgs) on Microsoft.AspNetCore.Components.Web.EventHandlers to recognize event attributes. Trimming removes that compile-time-only class, so handlers like @oninput=@(args => ...) become literal attributes and fail with CS1660 "Cannot convert lambda expression to type 'bool'". Preserve the assembly so demos compile online. -->
<assembly fullname="Microsoft.AspNetCore.Components.Web" preserve="all"/>
<!-- The following types are referenced only by demo source compiled at runtime (Edit Source / Playground), so trimming removes members the runtime compiler needs. Preserve them so those demos compile online. -->
<assembly fullname="System.Private.CoreLib">
<type fullname="System.Math" preserve="all"/>
</assembly>
<assembly fullname="System.ComponentModel.Annotations">
<type fullname="System.ComponentModel.DataAnnotations.DisplayAttribute" preserve="all"/>
</assembly>
<assembly fullname="Radzen.Blazor">
<type fullname="Radzen.Colors" preserve="all"/>
</assembly>
</linker>