referencing .NET framework 4 dll in .NET core 2.0

2019-08-23 07:23发布

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.

标签: c# dll .net-core
3条回答
相关推荐>>
2楼-- · 2019-08-23 07:47

In summary:

It doesn't work because the .NET Framework version of that DLL is too low. .NET Core 2.0 and up support referencing other .NET Standard dlls (so either .NET Core or .NET Framework, or whichever other future library might implement .NET Standard).

.NET Framework 4.0 does not implement .NET Standard, so it does not produce .NET Standard compatible dlls: .NET Framework only started implementing .NET Standard from version 4.5 and up. So if you can find a version of that DLL compiled for .NET Framework 4.5, it should theoretically work, barring some edge cases.

查看更多
别忘想泡老子
3楼-- · 2019-08-23 07:56

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 Standard 4.7.

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

查看更多
成全新的幸福
4楼-- · 2019-08-23 08:01

Is it even possible?

No, it's not.

  • .NET Core can run assemblies targeting either .NET Core itself or .NET Standard
  • .NET Framework can run assemblies targeting either .NET Framework itself or .NET Standard

So there is no way to bring two assemblies that target Full Framework and Core respectively together. Not in Core and not in full framework.

If you need a shared library that both can access, that would need to target .NET standard to be able to be used by both full framework and Core.

查看更多
登录 后发表回答