Is There a Way to Force a Project Reference to .NE

2020-02-28 02:22发布

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!

2条回答
戒情不戒烟
2楼-- · 2020-02-28 02:33

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.

查看更多
叼着烟拽天下
3楼-- · 2020-02-28 02:35

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.

查看更多
登录 后发表回答