在 .NET Core 3.0 中加载 exe 时出现错误的 IL 格式

Ali*_*tor 2 c# wpf .net-core

I have two .NET Core 3.0 WPF projects. One of them, I published into an executable called "DefaultPlugin.exe".

In my second WPF project, I'm trying to load the first project using Assembly.Load and read the types. However, when I do so, I get an exception, "Bad IL Format" from System.Private.CoreLib. It works when I load the DefaultPlugin.dll from the bin folder I also checked my CPU configuration to make sure they match.

///Works:
string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Debug\netcoreapp3.0\win-x64\DefaultPlugin.dll";
///Does not work:
//string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Debug\netcoreapp3.0\win-x64\DefaultPlugin.exe";
//string path = @"C:\Users\S-D81\source\repos\DefaultPlugin\DefaultPlugin\bin\Release\netcoreapp3.0\win-x64\publish\DefaultPlugin.exe";

Assembly assembly = Assembly.LoadFrom(path);
Run Code Online (Sandbox Code Playgroud)

Is there a way to use Assembly.Load on a published .NET Core 3.0 assembly or is this just a limitation of the new framework?

Mar*_*ich 5

.NET Core 3.0 应用程序中生成的 .exe 文件不是 IL 程序集,而是本机主机可执行文件,它在加载具有 .dll 扩展名的实际 IL 应用程序程序集之前查找并启动运行时。

您应该能够通过加载具有相同项目名称的 .exe 文件旁边的 .dll 文件来加载您的插件。