簡體   English   中英

如何從 .NET Framework 調用 .NET Core 程序集中的方法

[英]How to call a method in a .NET Core assembly from .NET Framework

我有兩個程序集:

  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 用於公共庫,您將能夠在 .NET Core 和 .NET Framework 中使用它。

答案很簡單:您不能直接從 .NET Framework 調用 .NET Core 程序集,反之亦然。 .NET Core 與 .NET Framework 不兼容。

您可以從 .NET Framework 程序集調用 .NET Core 的兼容程序集,只要該程序集的目標是使用 .NET Standard,最好至少是 NET Standard 2.0 以提供與 .NET Core 2.1 和 2.2 運行時的兼容性。

另請參閱 .NET Standard 的官方文檔: https ://learn.microsoft.com/en-us/dotnet/standard/net-standard

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM