CSharpCodeProvider.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #if NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP
  2. using Microsoft.CodeAnalysis;
  3. using Microsoft.CodeAnalysis.Emit;
  4. using Microsoft.CodeAnalysis.CSharp;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.Immutable;
  8. using System.IO;
  9. using System.Reflection;
  10. using System.Text;
  11. using FastReport.Code.CodeDom.Compiler;
  12. using System.Runtime.InteropServices;
  13. using System.Threading.Tasks;
  14. using System.Threading;
  15. namespace FastReport.Code.CSharp
  16. {
  17. public class CSharpCodeProvider : CodeDomProvider
  18. {
  19. protected override SyntaxTree ParseTree(string text, CancellationToken ct = default)
  20. => CSharpSyntaxTree.ParseText(text,
  21. cancellationToken: ct);
  22. private static CSharpCompilationOptions GetCompilationOptions()
  23. {
  24. CSharpCompilationOptions options = new CSharpCompilationOptions(
  25. OutputKind.DynamicallyLinkedLibrary,
  26. optimizationLevel: OptimizationLevel.Release,
  27. generalDiagnosticOption: ReportDiagnostic.Default,
  28. reportSuppressedDiagnostics: true);
  29. return options;
  30. }
  31. protected override Compilation CreateCompilation(SyntaxTree codeTree, ICollection<MetadataReference> references)
  32. {
  33. CSharpCompilationOptions options = GetCompilationOptions();
  34. Compilation compilation = CSharpCompilation.Create(
  35. "_" + Guid.NewGuid().ToString("D"), new SyntaxTree[] { codeTree },
  36. references: references, options: options
  37. );
  38. return compilation;
  39. }
  40. public override void Dispose()
  41. {
  42. }
  43. }
  44. }
  45. #endif