I am using VS 2019 Version 16.8.2

I referred a "CLR Class Library(.NET Core)" project in my "WPF App (.NET)" project and i met a exception:
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.
Let me first talk about how I did it.
Create a "WPF App (.NET)" project "TestCLR"

Add a new "CLR Class Library(.NET Core)" project "LibCLR" in this solution

The "common language runtime support" setting of "LibCLR"

Target "TestCLR" to ".NET 5.0"

"LibCLR.h" created automatically in project "LibCLR" and codes was inside
#pragma once
using namespace System;
namespace LibCLR {
public ref class Class1
{
// TODO: Add your methods for this class here.
};
}
using System.Windows;
namespace TestCLR {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
new LibCLR.Class1();
}
}
}
Rebuild All and debug, then i met exception

Then i change the solution platform to x64 or x84, the exception were the same.

It works well if i choice "WPF App (.NET Framework)" for "TestCLR" in step 1 and "CLR Class Library(.NET Framework)" for "LibCLR" in step 2
Why did not it work? Can not i use a "CLR Class Library(.NET)" project as a project reference in a "WPF App (.NET Core)" project? How can i solve this problem?
You should change Platform target to "X86" here

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With