diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs index 9dd92682..b742d166 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs @@ -5,6 +5,8 @@ using System.Diagnostics; using System.IO; using System.Reflection; +using System; +using System.Collections.Concurrent; #if NETCOREAPP3_0_OR_GREATER @@ -17,6 +19,22 @@ /// public sealed partial class AssemblyCSharpBuilder { + private ConcurrentQueue>? _emitOptionHandle; + /// + /// 处理默认生成的 emitOption 并返回一个新的 + /// + /// + /// + public AssemblyCSharpBuilder ConfigEmitOptions(Func handleAndReturnNewEmitOption) + { + if (_emitOptionHandle == null) + { + _emitOptionHandle = new ConcurrentQueue>(); + } + _emitOptionHandle.Enqueue(handleAndReturnNewEmitOption); + return this; + } + private bool _notLoadIntoDomain; /// /// 仅仅生成程序集,而不加载到域。 @@ -129,19 +147,31 @@ public Assembly GetAssembly() } debugInfoFormat = 0; } - - var compileResult = _compilation.Emit( - dllStream, - pdbStream: pdbStream, - xmlDocumentationStream: xmlStream, - options: new EmitOptions( + var emitOption = new EmitOptions( //runtimeMetadataVersion: Assembly.GetExecutingAssembly().ImageRuntimeVersion, //instrumentationKinds: [InstrumentationKind.TestCoverage], includePrivateMembers: _includePrivateMembers, metadataOnly: _isReferenceAssembly, pdbFilePath: PdbFilePath, debugInformationFormat: debugInfoFormat - ) + ); + + if (_emitOptionHandle != null) + { + while (!_emitOptionHandle.IsEmpty) + { + while (_emitOptionHandle.TryDequeue(out var func)) + { + emitOption = func(emitOption); + } + } + } + + var compileResult = _compilation.Emit( + dllStream, + pdbStream: pdbStream, + xmlDocumentationStream: xmlStream, + options: emitOption ); LogCompilationEvent?.Invoke(_compilation.GetNatashaLog()); Assembly? assembly = null;