简体   繁体   English

如何为C#类库多目标以同时支持NET_STANDARD和NET_COREAPP?

[英]How can I multi-target a C# Class Library to support both NET_STANDARD and NET_COREAPP?

I have a class library which is currently multi-targeting NET40 and NETSTANDARD2.0 : 我有一个类库,当前是多目标NET40NETSTANDARD2.0

<TargetFrameworks>net40;netstandard2.0</TargetFrameworks>

However, I now need to also support some new api which was added as part of NETCOREAPP2.1 which is not covered by netstandard . 但是,我现在还需要支持作为NETCOREAPP2.1一部分添加的一些新api,这不是netstandard涵盖的。

My initial thought is to simply expand the current frameworks to include NETCOREAPP2.1 : 我最初的想法是简单地将当前框架扩展为包括NETCOREAPP2.1

<TargetFrameworks>net40;netstandard2.0;netcoreapp2.1</TargetFrameworks>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.1'">
    <DefineConstants>NETCORE</DefineConstants> 
</PropertyGroup>

and in the code I can use the newly added API like so: 在代码中,我可以使用新添加的API,如下所示:

#if NETCORE
    // Use the api added in NETCORE 2.1
#endif

However what will happen once the library is used in the app targeting later versions of .NET Core ? 但是,一旦将库用于面向更高版本.NET Core的应用程序中,该怎么办? eg .NET Core 2.2 ? 例如.NET Core 2.2 Do I have to create new constants for every newly released version? 是否必须为每个新发行的版本创建新的常量?

In an ideal world NETCOREAPP2.1 would implement NETSTANDARD2.1 but unfortunately THIS is not the case. 在一个理想的世界NETCOREAPP2.1将实施NETSTANDARD2.1但不幸的是是不是这样的。

just create a .net standard 2.0 library. 只需创建一个.net标准2.0库。 this lib can be used by .net (>= 4.6.1) or core (>= 2.0) applications. .net(> = 4.6.1)或核心(> = 2.0)应用程序可以使用此库。 the downwards compatibility of an lib is guaranteed. 库的向下兼容性得到保证。

https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-platforms-support https://docs.microsoft.com/zh-cn/dotnet/standard/net-standard#net-platforms-support

You can use .Net string methods inside your condition: 您可以在条件内使用.Net字符串方法:

<PropertyGroup Condition="$(TargetFramework.StartsWith('netcore'))">
    <DefineConstants>NETCORE</DefineConstants> 
</PropertyGroup>

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

相关问题 .NET多目标与.NET标准和可移植库MSBuild 15 - .NET Multi-Target with .NET Standard and Portable Library MSBuild 15 如何在同一解决方案中从4.6.1项目引用多目标.NET Core类库? - How do I reference a multi-target .NET Core class library from a 4.6.1 project within the same solution? 如何使用 csproj 多目标 .NET Core 类库? - How do you multi-target a .NET Core class library with csproj? 如何通过仅使用.NET Core SDK直接调用C#编译器来编译.NET Standard 2.0类库? - How can I compile a .NET Standard 2.0 class library by directly invoking the C# complier using only the .NET Core SDK? 如何将 .NET 标准库更改为 .NET 框架库? - How can I change a .NET standard library to a .NET framework library? 如何使用MSBuild 15打包多目标程序包? - How I pack a multi-target package with MSBuild 15? Autofac无法从Multi Target .Net Standard库加载模块 - Autofac is not loading module from Multi Target .Net Standard library 我应该使用类库来定位.NET Framework和C#版本? - What .NET Framework and C# version should I target with my class library? 我可以同时在csproj文件中定位.net standard2.0和net471吗? - Can I target both .net standard2.0 and net471 in csproj file 我可以在 .NET Standard 类库中使用 dynamic 吗? - Can I use dynamic in a .NET Standard class library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM