将 .NET 8 程序集加载到 Matlab R2023a 时出现问题

问题描述 投票:0回答:1

我正在努力将 .NET 8、C# 动态库程序集加载到 Matlab 中。

我尝试过以下方法:

  1. 创建依赖于框架的库。这会导致 Matlab 找不到

    System.Runtime

    的错误
    Message: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
    Source: mscorlib
    HelpLink: None
    
  2. 创建一个独立的库。这似乎加载了一些 .NET 8 程序集,但最终失败并出现错误:

    Message: Could not load type 'System.Object' from assembly 'System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' because the parent does not exist.
    Source: mscorlib
    HelpLink: None
    
  3. 创建一个与 AOT 兼容的独立库。这会引发以下错误。警告中引用的 DLL 文件是我的库项目的 DLL。

    Message: Could not load file or assembly 'file:///<DllFilePath>' or one of its dependencies. The module was expected to contain an assembly manifest.
    Source: mscorlib
    HelpLink: None
    

尝试加载(任何加载)后,我检查 Matlab 中的 .NET 环境,得到以下结果:

>> dotnetenv

ans = 

  NETEnvironment with properties:

            Runtime: framework
             Status: loaded
            Version: ".NET Framework 4.0.30319.42000"
    RuntimeLocation: "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\"

如果程序集是 .NET 8,为什么还要加载 .NET Framework?

由于错误发生在加载过程中,因此 Visual Studio 调试器无法附加任何内容来使用类似

AssemblyResolve
的内容。我下载了 Fusion++ 并一直在尝试诊断 Matlab 尝试加载这些程序集的方式。

我想知道通过部分名称加载

System
命名空间是否是问题的一部分。由于它没有完全限定,是否有可能在 GAC 中查找 .NET Framework 程序集,而不是使用库提供的 .NET 8 程序集?有没有办法将 Matlab 加载器引导到正确的组件?

这是

System
命名空间的 Fusion 日志的摘录。

*** Assembly Binder Log Entry  (5/9/2024 @ 12:46:00) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = MATLAB.exe
Calling assembly : dotnetcli, Version=1.0.8570.32197, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.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: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/System.DLL.
LOG: Attempting download of new URL file:///<MatlabDir>/MATLAB/64_bit/R2023a/bin/win64/System/System.DLL.
LOG: Assembly download was successful. Attempting setup of file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\System\System.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
LOG: A partially-specified assembly bind succeeded from the application directory. Need to re-apply policy.
LOG: Using application configuration file: <MatlabDir>\MATLAB\64_bit\R2023a\bin\win64\MATLAB.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, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
LOG: Found assembly by looking in the GAC.
LOG: Binding succeeds. Returns assembly from C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll.
LOG: Assembly is loaded in default load context.
c# .net matlab interop
1个回答
0
投票

在 addassemly 之前调用 dotnetenv("core")

© www.soinside.com 2019 - 2024. All rights reserved.