c# - 如何从 .NET Framework 调用 .NET Core 程序集中的方法

标签 c# .net appdomain

我有两个程序集:

  1. 父程序集是针对 .NET Framework 编译的。
  2. 子程序集是针对 .NET Core 编译的。

我想在子程序集中调用一个方法,并检索它的返回对象。构造函数不接受任何参数。该方法不接受任何参数,并返回一个 Microsoft.OData.Edm.IEdmModel

到目前为止我尝试过的事情:

  1. 创建一个单独的应用程序域,并调用 domain.CreateInstanceAndUnwrap:
AppDomainSetup setup = new AppDomainSetup()
{
   ApplicationBase = path
   PrivateBinPath = path
};
AppDomain domain = AppDomain.CreateDomain("Child", null, setup);
AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyPath);
object instance = domain.CreateInstanceAndUnwrap(assemblyName.FullName, className);
  1. 创建一个扩展 MarshalByRefObject 的类,并使用它来加载程序集、调用函数并整理返回类型:
AppDomain domain = AppDomain.CreateDomain("Child");
BinaryMarshal marshal = (BinaryMarshal) domain.CreateInstanceAndUnwrap(typeof(BinaryMarshal).Assembly.FullName, typeof(BinaryMarshal).FullName);
IEdmModel model = marshal.LoadEdmModel(path, className, functionName);

//BinaryMarshal.cs:
internal class BinaryMarshal : MarshalByRefObject
{
    public IEdmModel LoadEdmModel(string binary, string className, string functionName)
    {
        Assembly assembly = Assembly.LoadFrom(binary);
        Type type = assembly.GetType(className);  //returns null because of exception listed below
     }
}

在这两种情况下,由于以下异常(从 Fuslogvw.exe 获得),代码都不起作用:

*** Assembly Binder Log Entry  (4/29/2019 @ 10:34:48 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\develop\parent\out\debug-amd64\Parent.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/develop/parent/out/debug-amd64/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Parent.exe
Calling assembly : Child, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\develop\parent\out\debug-amd64\Parent.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/parent/out/debug-amd64//System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/develop/child/out/debug-amd64/netcoreapp2.0/System.Runtime/System.Runtime.EXE.
LOG: All probing URLs attempted and failed.

据我所知,它正在尝试加载 System.Runtime,但失败了,因为父应用已经加载了不同版本的 System.Runtime。

这可能吗?

最佳答案

.NET Core 和 .NET Framework 是不同的运行时。尝试将 .NET Standard 用于公共(public)库,您将能够在 .NET Core 和 .NET Framework 中使用它。

关于c# - 如何从 .NET Framework 调用 .NET Core 程序集中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55907693/

相关文章:

c# - 如何以编程方式读取 .pdf 文件并将其转换为音频(.mp3 格式)?

appdomain - 程序集无法在影子复制的 AppDomain 中加载

c# - 创建 observablecollection 的过滤 View 并将其显示在列表框中

javascript - 当我加载下一页时,如何保持 toastr.js 通知打开?

.net - WindowsBase.dll 中发生了 'System.ComponentModel.Win32Exception' 类型的第一次机会异常

c# - 如何在C#或CS1503中使用通用变量

c# - 专门针对用户控件,在构造函数之后但在 Loaded 事件之前是否有一个地方可以访问 XAML 中设置的属性?

c# - 如何在本地托管 ASP.NET 应用程序

c# - 将一个对象传递给 Default AppDomain,以从进程中创建的子 AppDomain 接收回调

c# - 沙箱 AppDomain 跨程序集异常处理