Is There a Way to Force a Project Reference to .NET Standard Project to a Specific TargetFramework

╄→尐↘猪︶ㄣ 提交于 2019-12-18 15:03:56

问题


I am a contributor to a GitHub project, and recently we had some trouble with our .NET Standard 2.0 project installing correctly into a .NET Framework 4.5 project. The cause of this is that (if I am understanding correctly) .NET Standard 2.0 supports a minimum .NET Framework of 4.6.1.

OK, fair enough. So we updated the .csproj to create another framework output:

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

In our testing project, the supported frameworks are defined as such:

<TargetFrameworks>netcoreapp2.0;net471;net45</TargetFrameworks>

However, we are running into a problem with the net471 build as it seems to be picking up the net45 framework and not the netstandard2.0. In order to get this working, we are having to set the TargetFrameworks of the class library as such:

<TargetFrameworks>netstandard2.0;net471;net45</TargetFrameworks>

This seems excessive as it would seem that .netstandard2.0 should be the TargetFramework that net471 picks up, rather than the net45 target.

Is there a way to force a project reference to a particular TargetFramework? I did try the following in our testing project, but it did not seem work:

<ItemGroup Condition="'$(TargetFramework)' != 'net471'">
  <ProjectReference Include="..\..\src\ExtendedXmlSerializer\ExtendedXmlSerializer.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net471'">
  <ProjectReference Include="..\..\src\ExtendedXmlSerializer\ExtendedXmlSerializer.csproj">
    <TargetFramework>netstandard2.0</TargetFramework>
  </ProjectReference>
</ItemGroup>

Thank you in advance for any assistance you can provide!


回答1:


You can change your project reference like this:

  <ProjectReference Include="..\..\src\ExtendedXmlSerializer\ExtendedXmlSerializer.csproj" 
                    AdditionalProperties="TargetFramework=netstandard2.0" />

to force the selection of a specific target framework over the default "get nearest TFM" logic.




回答2:


It does not work. VS 2017 15.5.5. Reference from 4.6.2 MSTest project net462 target of multitarget (net462;netstandard2.0) class library. – SerG Feb 13 at 13:34

It is true, the new way to solve that is this:

<ProjectReference Include="..\multitargeted_lib\multitargeted_lib.csproj">
  <SetTargetFramework>TargetFramework=netstandard2.0</SetTargetFramework>
</ProjectReference>

Source is here.



来源:https://stackoverflow.com/questions/48526219/is-there-a-way-to-force-a-project-reference-to-net-standard-project-to-a-specif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!