@@ -23,31 +23,33 @@ public class LazyLoadingComplier
2323 // return complier.GetAssembly();
2424 //}
2525
26- /* 于2026-03-12注释:会导致单文件发布时 ,导航功能报错。
27- internal static Lazy<CSScriptLib.RoslynEvaluator> _compiler = new Lazy<CSScriptLib.RoslynEvaluator>(() =>
28- {
29- var compiler = new CSScriptLib.RoslynEvaluator();
30- compiler.DisableReferencingFromCode = false;
31- compiler
32- .ReferenceAssemblyOf<IFreeSql>()
33- .ReferenceDomainAssemblies();
34- return compiler;
35- });
26+ /*2026-3-13:应用单文件发布模式 ,导航功能报错。
27+ internal static Lazy<CSScriptLib.RoslynEvaluator> _compiler = new Lazy<CSScriptLib.RoslynEvaluator>(() =>
28+ {
29+ var compiler = new CSScriptLib.RoslynEvaluator();
30+ compiler.DisableReferencingFromCode = false;
31+ compiler
32+ .ReferenceAssemblyOf<IFreeSql>()
33+ .ReferenceDomainAssemblies();
34+ return compiler;
35+ });
3636
37- public static Assembly CompileCode(string cscode)
38- {
39- return _compiler.Value.CompileCode(cscode);
40- }
41- */
42- /*
43- * 于2026-03-12:删除CS-Script.Core库,改用官方的库 Microsoft.CodeAnalysis.CSharp。
44- */
45- private static readonly HashSet < MetadataReference > references = new HashSet < MetadataReference > ( from eve in AppDomain . CurrentDomain . GetAssemblies ( ) . AsParallel ( )
46- let ass = CreateMetadataReference ( eve )
47- where ass != null
48- select ass ) ;
37+ public static Assembly CompileCode(string cscode)
38+ {
39+ return _compiler.Value.CompileCode(cscode);
40+ }
41+ */
42+
43+ //2026-3-13:删除CS-Script.Core库,改用官方的库 Microsoft.CodeAnalysis.CSharp。
44+ private static readonly HashSet < MetadataReference > references = new ( ) ;
4945 static LazyLoadingComplier ( )
5046 {
47+ foreach ( var eve in AppDomain . CurrentDomain . GetAssemblies ( ) . AsParallel ( ) )
48+ {
49+ var ass = CreateMetadataReference ( eve ) ;
50+ if ( ass != null )
51+ references . Add ( ass ) ;
52+ }
5153 references . Add ( CreateMetadataReference ( typeof ( String ) . Assembly ) ) ;
5254 references . Add ( CreateMetadataReference ( Assembly . GetEntryAssembly ( ) ) ) ;
5355 references . Add ( CreateMetadataReference ( Assembly . GetCallingAssembly ( ) ) ) ;
@@ -57,20 +59,18 @@ static LazyLoadingComplier()
5759 public static Assembly CompileCode ( string cscode )
5860 {
5961 var tree = CSharpSyntaxTree . ParseText ( cscode ) ;
60- using ( var ms = new MemoryStream ( ) )
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 )
6168 {
62- var result = CSharpCompilation . Create ( "DynamicAssembly" )
63- . WithOptions ( new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary , optimizationLevel : OptimizationLevel . Release , reportSuppressedDiagnostics : false ) )
64- . AddReferences ( references )
65- . AddSyntaxTrees ( tree ) . Emit ( ms ) ;
66- if ( result . Success )
67- {
68- return Assembly . Load ( ms . ToArray ( ) ) ;
69- }
70- else
71- {
72- throw new Exception ( string . Join ( Environment . NewLine , from eve in result . Diagnostics select eve . ToString ( ) ) ) ;
73- }
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 ( ) ) ) ;
7474 }
7575 }
7676 public static MetadataReference CreateMetadataReference ( Assembly assembly )
@@ -88,6 +88,7 @@ public static MetadataReference CreateMetadataReference(Assembly assembly)
8888 }
8989 return null ;
9090 }
91+
9192#else
9293
9394 public static Assembly CompileCode ( string cscode )
0 commit comments