Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 EMIT 选项扩展处理 #242

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System;
using System.Collections.Concurrent;


#if NETCOREAPP3_0_OR_GREATER
Expand All @@ -17,6 +19,22 @@
/// </summary>
public sealed partial class AssemblyCSharpBuilder
{
private ConcurrentQueue<Func<EmitOptions, EmitOptions>>? _emitOptionHandle;
/// <summary>
/// 处理默认生成的 emitOption 并返回一个新的
/// </summary>
/// <param name="handleAndReturnNewEmitOption"></param>
/// <returns></returns>
public AssemblyCSharpBuilder ConfigEmitOptions(Func<EmitOptions, EmitOptions> handleAndReturnNewEmitOption)
{
if (_emitOptionHandle == null)
{
_emitOptionHandle = new ConcurrentQueue<Func<EmitOptions, EmitOptions>>();
}
_emitOptionHandle.Enqueue(handleAndReturnNewEmitOption);
return this;
}

private bool _notLoadIntoDomain;
/// <summary>
/// 仅仅生成程序集,而不加载到域。
Expand Down Expand Up @@ -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;
Expand Down
Loading