Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

referencing .NET framework 4 dll in .NET core 2.0

Tags:

c#

.net-core

dll

I have some dll that is written in .Net framework 4.0 and I can't run my program when I'm referencing it to my project which is written in .NET core 2.0.

Although my IDE (vs 2017) can recognize the objects imported from that dll correctly in run time Im having the following exception:

System.BadImageFormatException: 'Could not load file or assembly 'A_dotnet_4.0_A, Version=10.0.0.0, Culture=neutral, PublicKeyToken=0ad20d08c672086a'. An attempt was made to load a program with an incorrect format.'

I tried to:

  1. change my settings to any CPU as I saw in a post here
  2. tried to clean-rebuild my project.

Is it even possible? and if it does, how should I do so. In the following link is seem like it is possible - I just can't understand how.

like image 921
Green Avatar asked Oct 08 '18 13:10

Green


People also ask

Can I reference .NET Framework from .NET core?

NET Core, you need to use the . NET Framework. You can now reference . NET Framework libraries from .

Can I use .NET Framework DLL in .NET 6?

Yes, you can load . NET Framework assemblies into . NET Core 5 and 6.

How do I convert .NET Framework to .NET standard?

To do that, complete the following steps: In Visual Studio select Analyze and then Portability Analyzer Settings. In the General Settings window, select . NET Standard 2.0 under Target Platforms, and then choose OK.


1 Answers

You cannot do this.

.NET Core can reference a .NET Standard DLL
.NET Framework can reference a .NET Standard DLL

.NET Core cannot reference a .NET Framework DLL (or visa versa).

If you have for example a .NET Standard Project, you cannot reference .NET Framework and the .NET Core framework.
It's one or the other.

enter image description here

To further elaborate on this, we have a project that has shared BusinessLogic, that project is a .NET Standard 2.0 Library.
We reference that project in 2 other projects a.NET Core 2.1 and a .NET Framework 4.7.

Things go wrong when you reference .NET Core or .NET Framework items directly to that shared .NET Standard library.

like image 137
Hypenate Avatar answered Oct 03 '22 19:10

Hypenate