.net 加载编译的dll时无法加载文件或程序集System. dll,版本=7.0.0.0

gt0wga4j  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(251)

我有一个小的dll编译在Visual Studio中编写的C#(类库),我想在Matlab中使用以后。目前我的目标框架是.Net 7.0(但我也尝试过不同的版本)。我的最小例子包括代码:

namespace newTest
{
    public class Class1
    {
        public double add(double x, double y)
        {
            return x + y;
        }
    }
}

字符串
它编译得很好,但是当我想在Matlab中加载文件时,它会抛出以下错误:
使用newTestmatlab时出错未能加载文件或程序集“System.Web.Nov,Version=7.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。源:mscorlib
我知道有类似的问题,我已经尝试了很多不同的解决方案,但到目前为止都没有成功(在app.config中重新绑定,包括Nuget包中的System.config,不同的. NETsdk版本,安装其他.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)]


有人知道如何使它工作吗?我必须在编译或运行时方面做些什么吗?

4xrmg8kj

4xrmg8kj1#

我得到了同样的问题,直到我开始得到“尝试加载格式不正确的程序”后,试图通过CLI运行。我的问题是试图使用x86库与x64应用程序。仔细检查您的平台目标,并确保它们是相同的。

相关问题