1- using Microsoft . CodeAnalysis ;
2- using Microsoft . CodeAnalysis . CSharp ;
1+
32using System ;
43using System . CodeDom . Compiler ;
54using System . Collections . Generic ;
65using System . IO ;
76using System . Linq ;
87using System . Reflection ;
8+ #if ns20
9+ using Microsoft . CodeAnalysis ;
10+ using Microsoft . CodeAnalysis . CSharp ;
911using System . Reflection . Metadata ;
12+ #endif
1013
1114namespace FreeSql . Extensions . LazyLoading
1215{
@@ -41,7 +44,7 @@ public static Assembly CompileCode(string cscode)
4144 */
4245
4346 //2026-3-13:删除CS-Script.Core库,改用官方的库 Microsoft.CodeAnalysis.CSharp。
44- private static readonly HashSet < MetadataReference > references = new ( ) ;
47+ private static readonly HashSet < MetadataReference > references = new HashSet < MetadataReference > ( ) ;
4548 static LazyLoadingComplier ( )
4649 {
4750 foreach ( var eve in AppDomain . CurrentDomain . GetAssemblies ( ) . AsParallel ( ) )
@@ -59,18 +62,20 @@ static LazyLoadingComplier()
5962 public static Assembly CompileCode ( string cscode )
6063 {
6164 var tree = CSharpSyntaxTree . ParseText ( cscode ) ;
62- using var ms = new MemoryStream ( ) ;
63- var result = CSharpCompilation . Create ( "DynamicAssembly" )
64- . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary , optimizationLevel : OptimizationLevel . Release , reportSuppressedDiagnostics : false ) )
65- . AddReferences ( references )
66- . AddSyntaxTrees ( tree ) . Emit ( ms ) ;
67- if ( result . Success )
65+ using ( var ms = new MemoryStream ( ) )
6866 {
69- return Assembly . Load ( ms . ToArray ( ) ) ;
70- }
71- else
72- {
73- throw new Exception ( string . Join ( Environment . NewLine , from eve in result . Diagnostics select eve . ToString ( ) ) ) ;
67+ var result = CSharpCompilation . Create ( "DynamicAssembly" )
68+ . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary , optimizationLevel : OptimizationLevel . Release , reportSuppressedDiagnostics : false ) )
69+ . AddReferences ( references )
70+ . AddSyntaxTrees ( tree ) . Emit ( ms ) ;
71+ if ( result . Success )
72+ {
73+ return Assembly . Load ( ms . ToArray ( ) ) ;
74+ }
75+ else
76+ {
77+ throw new Exception ( string . Join ( Environment . NewLine , from eve in result . Diagnostics select eve . ToString ( ) ) ) ;
78+ }
7479 }
7580 }
7681 public static MetadataReference CreateMetadataReference ( Assembly assembly )
0 commit comments