简体   繁体   English

我可以从 .NET 6 项目中添加对 .NET 框架 DLL 的引用吗?

[英]Can I add a reference to a .NET Framework DLL from a .NET 6 project?

Microsoft recently announced.Net 6.0 as major version.微软最近宣布.Net 6.0 作为主要版本。 I didn't find any reference where we can use older .NET framework (> 4.7.*) references in .net 6.0 project?我没有在 .net 6.0 项目中找到可以使用旧 .NET 框架(> 4.7.*)参考的任何参考?

Say, a .NET 6 project will have dll reference of .NET Framework 4.7.1 for windows application?比如说,.NET 6 项目将具有 dll 参考 .NET 框架 4.7.1 用于 Z0F4137ED1502B5545DZ44A 应用程序?

In general, no you can't.一般来说,不,你不能。 They're not really designed to be compatible.它们并不是真正设计为兼容的。 .NET Core and the subsequent .NET 5 and 6 are based on a complete re-write of .NET. .NET Core 和后续的 .NET 5 和 6 是基于对 .NET 的完全重写。

There may be some chance of things which work using the .NET Framework Compatibility mode but it's far from guaranteed.使用.NET 框架兼容模式可能会有一些事情发生,但这远不能保证。

If you want reliable, cross-framework compabitility for your libraries between .NET Framework and .NET Core / 6 you can try to target your libraries at .NET Standard如果您希望在 .NET 框架和 .NET Core / 6 之间为您的库提供可靠的跨框架兼容性,您可以尝试将您的库定位在.NET 标准

This is a question with a complex answer.这是一个答案很复杂的问题。

Let me blunt.让我直言不讳。 It is highly likely the answer to your question will be no, it can't be done, but that is because you have a very specific library in mind.您的问题的答案很可能是否定的,无法完成,但那是因为您有一个非常具体的库。 The real answer is that it depends on the library in question, but chances are mostly geared towards this not working.真正的答案是它取决于所讨论的库,但机会大多是针对这个不起作用的。

Yes, you can load .NET Framework assemblies into .NET Core 5 and 6.是的,您可以将 .NET 框架组件加载到 .NET 核心 5 和 6 中。

However, depending on what that library does, and probably more important, what dependencies it has aka other libraries it wants to drag with it, it might not work properly for any sizable complex library.但是,取决于该库的功能,并且可能更重要的是,它具有哪些依赖项,也就是它想要与它一起拖动的其他库,对于任何大型复杂库,它可能无法正常工作。

There are classes that doesn't exist in .NET 5+, or even just single method overloads or properties. .NET 5+ 中不存在某些类,甚至只是单个方法重载或属性。 Depending on the exact parts of .NET Framework you access, it may in fact just be missing outright in .NET 5+.根据您访问的 .NET 框架的确切部分,它实际上可能在 .NET 5+ 中完全丢失。

There is more information here . 这里有更多信息。

Your best option is probably to try to:您最好的选择可能是尝试:

  • Re-compile the library for your particular .NET version (be it 5 or 6)为您的特定 .NET 版本(无论是 5 还是 6)重新编译库
  • Re-compile the library targeting .NET Standard 2.1, as .NET 5+ is compatible with that重新编译针对 .NET 标准 2.1 的库,因为 .NET 5+ 与该库兼容

However, you might in fact just get into the exact same problem with that re-compile as you will have to deal with those changed bits.但是,实际上您可能会在重新编译时遇到完全相同的问题,因为您必须处理那些更改的位。 But, then at least you would have more knowledge about what would and wouldn't work, and you have a chance to fix it.但是,至少你会对什么会起作用和不会起作用有更多的了解,并且你有机会修复它。

If it's not your library to change, either replace it, or try and hope for the best.如果不是您的库要更改,请替换它,或者尝试并希望最好。

The Problem问题

Assemblies that "target" (are compiled against/for) .NET Framework (1.0-4.8) may use APIs (types, methods) that are not present in .NET Standard or .NET Core. “目标”(针对/针对).NET 框架(1.0-4.8)的程序集可能使用 .NET 标准或 .NET 核心中不存在的 API(类型、方法)。

So if you have a project that targets .NET Standard or .NET Core, and you want to add a reference to an assembly that targets .NET Framework, it may be that the assembly will throw exceptions at runtime, because it's missing method overloads or types.因此,如果您有一个针对 .NET 标准或 .NET 核心的项目,并且您想要添加对针对 .NET 框架的程序集的引用,则可能是程序集方法会在运行时引发缺少类型的重载或异常。 Those types are present in the Framework DLLs, but not in the .NET Core runtime assemblies.这些类型存在于框架 DLL 中,但不存在于 .NET 核心运行时程序集中。

Now if you know (through interpreting the code or through testing, preferably both) that the Framework-targeting assembly doesn't use APIs that are missing from .NET Standard or .NET Core, you're fine (for the difference, see What is the difference between .NET Core and .NET Standard Class Library project types? ).现在,如果您知道(通过解释代码或通过测试,最好两者兼而有之)框架定位程序集不使用 .NET 标准或 .NET 核心中缺少的 API,那么您很好(对于区别,请参阅What is the .NET Core 和 .NET 标准 Class 库项目类型之间的区别? )。

The fix (but not really)修复(但不是真的)

If it's an assembly that's chucked in a lib folder in source control, you can add an assembly reference:如果它是在源代码管理的lib文件夹中的程序集,您可以添加程序集引用:

<Reference Include="../lib/path/To/Dll.dll" />

If it's a NuGet-packed dependency, you can install it and override the warning :如果它是 NuGet 打包的依赖项,您可以安装它并覆盖警告

<PackageReference Include="Some.Framework.Package" Version="1.0.0" NoWarn="NU1701" />

The fix (for real this time)修复(这次是真的)

Recompile the assembly to target .NET Standard 2.0, and package and distribute that through NuGet.重新编译程序集以定位 .NET 标准 2.0 和 package 并通过 NuGet 分发。

You can have a .NET 6 Core application calling into a .NET Framework 4.8 dll.您可以让 .NET 6 核心应用程序调用 .NET 框架 4.8 dll。 It took me a while to figure out how to do it, but it can be done.我花了一段时间才弄清楚如何做到这一点,但这是可以做到的。 I have created both console and web .NET 6 Core applications which call into some of my legacy .NET Framework 4.8 dlls.我已经创建了控制台和 web .NET 6 核心应用程序,它们调用了我的一些旧版 .NET 框架 4.8 dll。 The process is as follows:过程如下:

  • Create or open your .NET 6 Core solution in Visual Studio 2022 and add your legacy .NET Framework 4.8 dll to your .NET 6 Core solution.在 Visual Studio 2022 中创建或打开您的 .NET 6 核解决方案,并将您的旧版 .NET 框架 4.8 dll 添加到您的 Z3061660EF9EDB90972DAZ 解决方案中。
  • Add your legacy .NET Framwork 4.8 dll as a reference to your top level .NET 6 Core Console or Web project.添加您的旧版 .NET 框架 4.8 dll 作为顶级 .NET 6 核心控制台或 ZC6E190B254633C48E 项目的参考
  • Delete the "obj" and "bin" directories from your top level project via file explorer.通过文件资源管理器从顶级项目中删除“obj”和“bin”目录。
  • From the Visual Studio 2022 top level menu, select "Tools > Command Line > Developer Command Prompt".在 Visual Studio 2022 顶级菜单中,select“工具 > 命令行 > 开发人员命令提示符”。 This should open a command line window with the current directory changed to that of your solution.这应该会打开一个命令行 window 并将当前目录更改为您的解决方案的目录。
  • Run the command "dotnet restore" in the command window.运行命令 window 中的命令“dotnet restore”。 This will re-create your "obj" directory and place a "project.assets.JSON" file into the "obj" directory which will reference your recently added .NET Frameowork 4.8 dll.这将重新创建您的“obj”目录并将“project.assets.JSON”文件放入“obj”目录中,该目录将引用您最近添加的 .NET 框架 4.8 dll。 If you fail to do this you will get a runtime error when you run up your application.如果你不这样做,当你运行你的应用程序时,你会得到一个运行时错误。 Rebuild and rerun your application.重建并重新运行您的应用程序。 Your .NET Framework DLL should now load with no problems.您的 .NET 框架 DLL 现在应该可以正常加载。

I'm not sure if the removal of the "obj" and "bin" directories is always essential, however I have found that if you don't remove them, occasionally the "project.assets.JSON" file isn't generated properly and running fails.我不确定删除“obj”和“bin”目录是否总是必不可少的,但是我发现如果不删除它们,有时“project.assets.JSON”文件不会正确生成并且运行失败。 Removing them then re-running the command solves the problem.删除它们然后重新运行命令可以解决问题。

Hope that helps,希望有帮助,

Best wishes,最好的祝愿,

Lloyd.劳埃德。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM