gpt4 book ai didi

dll - C# CLR 异常 "BadImageFormatException: Could not load file or assembly"

转载 作者:行者123 更新时间:2023-12-05 07:00:50 25 4
gpt4 key购买 nike

我正在使用 VS 2019 版本 16.8.2 enter image description here

我在我的“WPF App (.NET)”项目中引用了一个“CLR 类库(.NET Core)”项目,但我遇到了一个异常:

BadImageFormatException: Could not load file or assembly 'LibCLR, Version=1.0.7646.21580, Culture=neutral, PublicKeyToken=null'. An attempt was made to load a program with an incorrect format.

先说说我是怎么做到的。

  1. 创建“WPF App (.NET)”项目“TestCLR” enter image description here

  2. 在此解决方案中添加一个新的“CLR 类库(.NET Core)”项目“LibCLR” enter image description here

  3. “LibCLR”的“公共(public)语言运行时支持”设置 enter image description here

  4. 将“TestCLR”定位到“.NET 5.0” enter image description here

  5. 在“LibCLR”项目中自动创建“LibCLR.h”,代码在里面

#pragma once

using namespace System;

namespace LibCLR {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
  1. 添加“新 LibCLR.Class1();”在“TestCLR”项目的 MainWindow.xaml.cs 中
using System.Windows;

namespace TestCLR {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
new LibCLR.Class1();
}
}
}
  1. 全部重建并调试,然后我遇到了异常 enter image description here

  2. 然后我将解决方案平台更改为 x64 或 x84,异常(exception)情况相同。 enter image description here

  3. 如果我在步骤 1 中为“TestCLR”选择“WPF App (.NET Framework)”,在步骤 2 中为“LibCLR”选择“CLR Class Library(.NET Framework)”,效果会很好

为什么没有效果?我不能在“WPF App (.NET Core)”项目中使用“CLR Class Library(.NET)”项目作为项目引用吗?我该如何解决这个问题?

最佳答案

您应该在此处将平台目标更改为“X86” enter image description here

关于dll - C# CLR 异常 "BadImageFormatException: Could not load file or assembly",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64025144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com