1- using System ;
2- using System . CodeDom . Compiler ;
1+ using Microsoft . CodeAnalysis ;
2+ using Microsoft . CodeAnalysis . CSharp ;
3+ using System ;
4+ using System . Collections . Generic ;
35using System . IO ;
6+ using System . Linq ;
47using System . Reflection ;
8+ using System . Reflection . Metadata ;
59
610namespace FreeSql . Extensions . LazyLoading
711{
@@ -10,28 +14,80 @@ public class LazyLoadingComplier
1014 {
1115
1216#if ns20
13- //public static Assembly CompileCode(string cscode)
14- //{
15- // Natasha.AssemblyComplier complier = new Natasha.AssemblyComplier();
16- // //complier.Domain = DomainManagment.Random;
17- // complier.Add(cscode);
18- // return complier.GetAssembly();
19- //}
20-
21- internal static Lazy < CSScriptLib . RoslynEvaluator > _compiler = new Lazy < CSScriptLib . RoslynEvaluator > ( ( ) =>
22- {
23- var compiler = new CSScriptLib . RoslynEvaluator ( ) ;
24- compiler . DisableReferencingFromCode = false ;
25- compiler
26- . ReferenceAssemblyOf < IFreeSql > ( )
27- . ReferenceDomainAssemblies ( ) ;
28- return compiler ;
29- } ) ;
17+ //public static Assembly CompileCode(string cscode)
18+ //{
19+ // Natasha.AssemblyComplier complier = new Natasha.AssemblyComplier();
20+ // //complier.Domain = DomainManagment.Random;
21+ // complier.Add(cscode);
22+ // return complier.GetAssembly();
23+ //}
24+
25+ /*2026-3-13:应用单文件发布模式,导航功能报错。
26+ internal static Lazy<CSScriptLib.RoslynEvaluator> _compiler = new Lazy<CSScriptLib.RoslynEvaluator>(() =>
27+ {
28+ var compiler = new CSScriptLib.RoslynEvaluator();
29+ compiler.DisableReferencingFromCode = false;
30+ compiler
31+ .ReferenceAssemblyOf<IFreeSql>()
32+ .ReferenceDomainAssemblies();
33+ return compiler;
34+ });
35+
36+ public static Assembly CompileCode(string cscode)
37+ {
38+ return _compiler.Value.CompileCode(cscode);
39+ }
40+ */
41+
42+ //2026-3-13:删除CS-Script.Core库,改用官方的库 Microsoft.CodeAnalysis.CSharp。
43+ private static readonly HashSet < MetadataReference > references = new ( ) ;
44+ static LazyLoadingComplier ( )
45+ {
46+ foreach ( var eve in AppDomain . CurrentDomain . GetAssemblies ( ) . AsParallel ( ) )
47+ {
48+ var ass = CreateMetadataReference ( eve ) ;
49+ if ( ass != null )
50+ references . Add ( ass ) ;
51+ }
52+ references . Add ( CreateMetadataReference ( typeof ( String ) . Assembly ) ) ;
53+ references . Add ( CreateMetadataReference ( Assembly . GetEntryAssembly ( ) ) ) ;
54+ references . Add ( CreateMetadataReference ( Assembly . GetCallingAssembly ( ) ) ) ;
55+ references . Add ( CreateMetadataReference ( Assembly . GetExecutingAssembly ( ) ) ) ;
56+ references . Add ( CreateMetadataReference ( typeof ( FreeSql . FreeSqlBuilder ) . Assembly ) ) ;
57+ }
58+ public static Assembly CompileCode ( string cscode )
59+ {
60+ var tree = CSharpSyntaxTree . ParseText ( cscode ) ;
61+ using var ms = new MemoryStream ( ) ;
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+ }
74+ }
75+ public static MetadataReference CreateMetadataReference ( Assembly assembly )
76+ {
77+ if ( ! string . IsNullOrEmpty ( assembly . Location ) )
78+ return MetadataReference . CreateFromFile ( assembly . Location ) ;
79+
80+ unsafe
81+ { // 纯内存 Assembly(.NET 5+)
82+ if ( assembly . TryGetRawMetadata ( out byte * blob , out int length ) )
83+ {
84+ var moduleMetadata = ModuleMetadata . CreateFromMetadata ( ( IntPtr ) blob , length ) ;
85+ return AssemblyMetadata . Create ( moduleMetadata ) . GetReference ( ) ;
86+ }
87+ }
88+ return null ;
89+ }
3090
31- public static Assembly CompileCode ( string cscode )
32- {
33- return _compiler . Value . CompileCode ( cscode ) ;
34- }
3591#else
3692
3793 public static Assembly CompileCode ( string cscode )
0 commit comments