[英]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
: 我有一个类库,当前是多目标
NET40
和NETSTANDARD2.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.