我为这个问题奋斗了好几个小时,但到处都找不到答案。我正在使用Roslyn加载一个文件,将一些代码编译成一个汇编。我已经明确地加载了 MetadataReference
s,运行 compilation.Emit
并将产生的内存流加载到一个汇编中。但当我尝试 GetTypes()
汇编中,它找不到我明确加载的引用。与单个 GetType()
. 该 LoaderExceptions
显示它找错了地方。
private string CompileAndExecuteCodeTest(string inputFileName, string inputFileContent)
{
var references = new List<MetadataReference> { [removed for brevity, multiple referenced] };
var sourceLanguage = new CSharpLanguage(references);
var syntaxTree = sourceLanguage.ParseText(inputFileContent, SourceCodeKind.Regular);
var assemblyName = Path.GetRandomFileName() + ".generated.dll";
var compilation = sourceLanguage
.CreateLibraryCompilation(assemblyName: assemblyName, enableOptimisations: false)
.AddReferences(references)
.AddSyntaxTrees(syntaxTree);
var stream = new MemoryStream();
var emitResult = compilation.Emit(stream);
if (emitResult.Success)
{
stream.Seek(0, SeekOrigin.Begin);
var assembly = Assembly.Load(stream.GetBuffer());
var types = assembly.GetTypes(); //// <<<<<< ERROR HERE
...
}
...
}
LoaderException信息。
=== Pre-bind state information ===
LOG: DisplayName = MyTestProject, Version=0.1.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\username\AppData\Local\Microsoft\VisualStudio\16.0_0adcfb3eExp\devenv.exe.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PublicAssemblies/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PublicAssemblies/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/TestWindow/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/TestWindow/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Platform/Debugger/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Platform/Debugger/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/MyTestProject/MyTestProject.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PublicAssemblies/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PublicAssemblies/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/TestWindow/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/TestWindow/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Platform/Debugger/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Platform/Debugger/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/MyTestProject/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/MyTestProject.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/Common7/IDE/PrivateAssemblies/DataCollectors/x86/MyTestProject/MyTestProject.EXE.
我最终使用了Ayende Rahien ( ) 的代码。http:/ayende.comlog1376solving theassembly-load-context-problem。)和手动解析 CurrentDomain_AssemblyResolve
. 我还是不明白,为什么他们没有自动根据 MetadataReference.CreateFromFile
但代码还在工作和编译中。
为了防止有一天它消失了,这里是代码。
public static class AssemblyLocator
{
static Dictionary<string, Assembly>assemblies;
public static void Init()
{
assemblies = new Dictionary<string, Assembly>();
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly assembly = null;
assemblies.TryGetValue(args.Name, out assembly);
return assembly;
}
static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
Assembly assembly = args.LoadedAssembly;
assemblies[assembly.FullName] = assembly;
}
}