简体   繁体   English

我可以在经典的 .NET 4.6.1 应用程序中使用 .NET Standard 2.0 包吗?

[英]Can I use a .NET Standard 2.0 package in a classic .NET 4.6.1 application?

I'm a bit confusing how to share .net-standard 2 nuget package between .net core and .net-framework applications.我有点困惑如何在.net core.net-framework应用程序之间共享.net-standard 2 nuget 包。 For instance, I created .net-standard 2.0 package with only <TargetFramework>netstandard2.0</TargetFramework> .例如,我创建了.net-standard 2.0包,只有<TargetFramework>netstandard2.0</TargetFramework> So I have only ../libs/netstandard2.0 folder in package.所以我在包中只有../libs/netstandard2.0文件夹。 Can I install it as is into .net-framework 4.6.1.我可以将它按原样安装到 .net-framework 4.6.1 中吗? application (according to compatibility matrix I can)?应用程序(根据 兼容性矩阵我可以)? Or should I make this package multi target <TargetFrameworks>netstandard2.0;net461</TargetFrameworks> ?或者我应该让这个包成为多目标<TargetFrameworks>netstandard2.0;net461</TargetFrameworks> What advantages here?这里有什么优势?

Yes, indeed.确实是的。 You can just reference the .NET Standard 2.0 package from within a .NET Framework 4.6.1 project (a framework version lower will not work with .NET Standard 2.0).您可以仅从 .NET Framework 4.6.1 项目中引用 .NET Standard 2.0 包(较低的框架版本将不适用于 .NET Standard 2.0)。 There is no need to have separate targets or rebuilds.不需要有单独的目标或重建。

Also read Referencing .NET Standard Assemblies from both .NET Core and .NET Framework .另请阅读从 .NET Core 和 .NET Framework 中引用 .NET 标准程序集

About the benefits of using multi-targeting: there is none unless you need to.关于使用多目标的好处:除非你需要,否则没有。 What can be such a case?这种情况怎么办? According to the documentation :根据文档

You may need to target older versions of the .NET Framework when your project supports both the .NET Framework and .NET Core.当您的项目同时支持 .NET Framework 和 .NET Core 时,您可能需要面向较旧版本的 .NET Framework。 In this scenario, if you want to use newer APIs and language constructs for the newer targets, use #if directives in your code.在这种情况下,如果要为较新的目标使用较新的 API 和语言构造,请在代码中使用 #if 指令。 You also might need to add different packages and dependencies for each platform you're targeting to include the different APIs needed for each case.您可能还需要为每个目标平台添加不同的包和依赖项,以包含每种情况所需的不同 API。

So you would only really need that if you have incompatibility in code or references.因此,只有在代码或引用不兼容时才真正需要它。 If you don't have that problem, you don't need multi-targeting.如果你没有这个问题,你就不需要多目标。

There is a sample given how to include references based on the build target, which might give some insight:有一个示例给出了如何根据构建目标包含引用,这可能会提供一些见解:

<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
  <Reference Include="System.Net" />
</ItemGroup>

暂无
暂无

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

相关问题 我可以使用在.NET Standard 2.0中为.NET Framework编写的包吗? - Can I use packages written for .NET Framework in .NET Standard 2.0? 当我将.NET Standard 2.0 DLL库与.NET Framework控制台应用程序一起使用时,如何设置依赖关系? - How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application? 我可以在.Net Framework 4.6.1中使用C#7.3吗? - Can I use C# 7.3 with .Net Framework 4.6.1? 如何在 Visual Studio 2022 和 .net Standard 2.0 或 .net 4.8 中使用 CallerArgumentExpression? - How can I use CallerArgumentExpression with Visual Studio 2022 and .net Standard 2.0 or .net 4.8? 我可以在标准.Net应用程序中使用PCL库吗? - Can I use PCL library in standard .Net application? 引用.NET Standard 2.0库的.NET 4.6.1项目-VSTS不会构建 - .NET 4.6.1 project referencing a .NET Standard 2.0 library - VSTS wont build 我可以在.NET 2.0或.NET 3.5应用程序中使用Math.NET运行时吗? - Can I use Math.NET runtimes with a .NET 2.0 or .NET 3.5 application? 适用于CRM REST的.NET Standard 2.0 Nuget软件包 - .NET Standard 2.0 Nuget package for CRM REST 如何使用Asp.Net.Identity和Entity Framework 6在Asp.Net Core 2.0 Target Framework 4.6.1中设置身份? - How can I setup identity in Asp.Net Core 2.0 Target Framework 4.6.1 using Asp.Net.Identity and Entity Framework 6? 如何在.NET Standard 2.0项目上实现Dotfuscator? - How can I implement Dotfuscator on a .NET Standard 2.0 project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM