我有一个用 C#(类库)编写的 Visual Studio 编译的小 dll,我想稍后在 Matlab 中使用它。 目前我的目标框架是.Net 7.0(但我也尝试过不同的版本)。 我所包含的代码的最小示例是:
namespace newTest
{
public class Class1
{
public double add(double x, double y)
{
return x + y;
}
}
}
它编译得很好,但是当我想在 Matlab 中加载文件时,它会抛出以下错误:
使用 newTestmatlab 时出错 消息:无法加载文件或程序集“System.Runtime,版本=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其之一 依赖关系。该系统找不到指定的文件。 来源:mscorlib
我知道有类似的问题,我已经尝试了很多不同的解决方案,但到目前为止没有任何效果(在app.config中重新绑定,在Nuget包中包含System.Runtime,不同的.NET sdk版本,安装其他.NET运行时版本)
构建输出:
Rebuild started...
1>------ Rebuild All started: Project: newTest, Configuration: Debug Any CPU ------
Restoring NuGet packages...
To prevent NuGet from restoring packages during build, open the Visual Studio Options dialog, click on the NuGet Package Manager node and uncheck 'Allow NuGet to download missing packages during build.'
Restoring packages for C:\Users\42ggeissler\source\repos\newTest\newTest.csproj...
Assets file has not changed. Skipping assets file writing. Path: C:\Users\42ggeissler\source\repos\newTest\obj\project.assets.json
Restored C:\Users\42ggeissler\source\repos\newTest\newTest.csproj (in 11 ms).
NuGet package restore finished.
1>Build started 11/14/2023 11:07:42 AM.
1>Target CoreClean:
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.deps.json".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.dll".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.pdb".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.GeneratedMSBuildEditorConfig.editorconfig".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.AssemblyInfoInputs.cache".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.AssemblyInfo.cs".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.csproj.CoreCompileInputs.cache".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.dll".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\refint\newTest.dll".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.pdb".
1> Deleting file "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\ref\newTest.dll".
1>Target GenerateTargetFrameworkMonikerAttribute:
1> Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>Target CoreCompile:
1> CompilerServer: server - server processed compilation - 54a7adbc-1a49-448f-9523-516a579659d8
1>Target CopyFilesToOutputDirectory:
1> Copying file from "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.dll" to "C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.dll".
1> Copying reference assembly from "obj\Debug\net7.0\refint\newTest.dll" to "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\ref\newTest.dll".
1> newTest -> C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.dll
1> Copying file from "C:\Users\42ggeissler\source\repos\newTest\obj\Debug\net7.0\newTest.pdb" to "C:\Users\42ggeissler\source\repos\newTest\bin\Debug\net7.0\newTest.pdb".
1>
1>Build succeeded.
1> 0 Warning(s)
1> 0 Error(s)
1>
1>Time Elapsed 00:00:00.78
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
========== Rebuild started at 11:07 AM and took 01.203 seconds ==========
dotnet --info
的输出是:
.NET SDK:
Version: 7.0.403
Commit: 142776d834
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.403\
Host:
Version: 7.0.13
Architecture: x64
Commit: 3f73a2f186
.NET SDKs installed:
7.0.403 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
使用 IlSpy 检查 dll:
// C:\Users\user\source\repos\newTest\bin\Debug\net7.0\newTest.dll
// newTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: v4.0.30319
// This assembly was compiled using the /deterministic option.
// Hash algorithm: SHA1
// Debug info: Loaded from portable PDB: C:\Users\user\source\repos\newTest\bin\Debug\net7.0\newTest.pdb
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: TargetFramework(".NETCoreApp,Version=v7.0", FrameworkDisplayName = ".NET 7.0")]
[assembly: AssemblyCompany("newTest")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("newTest")]
[assembly: AssemblyTitle("newTest")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
有人知道如何让它发挥作用吗?我需要在编译或运行时方面做一些事情吗?
我遇到了同样的问题,直到在尝试运行 CLI 后开始收到“尝试加载格式不正确的程序”为止。我的问题是尝试将 x86 库与 x64 应用程序一起使用。仔细检查您的平台目标并确保它们是相同的。