[英]Which target framework should I choose for library project in .NET Core
My current mindset is to use .NET Standard xx for libraries and concrete frameworks for executable projects.我目前的想法是将 .NET Standard xx 用于可执行项目的库和具体框架。 So with this in mind let's say I have one common (fictional) project
Common.csproj
因此,考虑到这一点,假设我有一个常见的(虚构的)项目
Common.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="FluentValidation" Version="9.5.4" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="FluentValidation" Version="8.6.0" />
</ItemGroup>
One library project Lib.csproj
一个库项目
Lib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="Common.csproj" />
</ItemGroup>
And one executable project Cli.csproj
和一个可执行项目
Cli.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="Lib.csproj" />
</ItemGroup>
So, in this case if FluentValidation
library has different method signatures in different versions, compilation in some cases goes thru, but you get error at runtime.因此,在这种情况下,如果
FluentValidation
库在不同版本中具有不同的方法签名,则在某些情况下编译会通过,但在运行时会出错。
What are the recommendations for cases like this?对这种情况有什么建议? Should I just support all needed target frameworks in
Lib.csproj
?我应该只支持
Lib.csproj
所有需要的目标框架吗?
I think that what you're after is using pre-processor directives with target frameworks:我认为您所追求的是在目标框架中使用预处理器指令:
#if NETSTANDARD
// Code targeting .Net Standard
#elif NET
// Code targetting .Net Core / .Net
#elif NETFRAMEWORK
// Code targetting .Net Framework
#endif
You can get as specific as you like ie targeting .Net Standard 2.0 exactly:您可以随心所欲地获得具体目标,即准确定位 .Net Standard 2.0:
#if NETSTANDARD2_0
// .Net Standard 2.0 specific code
#elif
// anything else!
#endif
This will let Common.csproj
use the correct method calls for the appropriate version of FluentValidation that you need.这将使
Common.csproj
使用正确的方法调用来调用您需要的相应版本的 FluentValidation。
Personally, I find it a little messy - but sometimes, needs must.就个人而言,我觉得它有点凌乱 - 但有时,需要必须。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.