简体   繁体   English

Visual Studio - 多目标时更改目标框架

[英]Visual Studio - Change target framework when multitargeting

IS there a way to change what framework a project (that has multiple target frameworks) is compiled in without updating the csproj?有没有办法在不更新 csproj 的情况下更改编译项目(具有多个目标框架)的框架?

I'm writing a NuGet package that supports both .NET 4.8 and .NET 6. (I can't use .NET Standard 2.0.)我正在编写一个同时支持 .NET 4.8 和 .NET 6 的 NuGet 包。(我不能使用 .NET Standard 2.0。)

I have files that I want to be compiled only when targeting .NET 4.8 and others that should only be compiled when targeting .NET 6.0.我有一些文件,我只想在面向 .NET 4.8 时编译,而其他文件只应在面向 .NET 6.0 时编译。 I know there's a couple ways to achieve this, but I am trying to structure my files such that the directory /AspNetCore contains all of my .NET 6 files and /NetFramework contains all my .NET 4.8 files.我知道有几种方法可以实现这一点,但我正在尝试构建我的文件,使目录/AspNetCore包含我所有的 .NET 6 文件,而/NetFramework包含我所有的 .NET 4.8 文件。

With that, in my csproj, I can do:有了它,在我的 csproj 中,我可以这样做:

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
  <Compile Remove="NetFramework\**" />
  <EmbeddedResource Remove="NetFramework\**" />
  <None Remove="NetFramework\**" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net48'">
    <Compile Remove="AspNetCore\**" />
    <EmbeddedResource Remove="AspNetCore\**" />
    <None Remove="AspNetCore\**" />
</ItemGroup>

But then the /AspNetCore folder is hidden because the IDE is choosing NET48 as the target framework.但是 /AspNetCore 文件夹被隐藏了,因为 IDE 选择 NET48 作为目标框架。 This is fine when I'm working on the NET48 code, but not ideal when I'm working on the NET6.0 code.这在我处理 NET48 代码时很好,但在我处理 NET6.0 代码时就不理想了。

在此处输入图像描述

If I were using #if NETFRAMEWORK #elif NET6_0 #endif syntax in each file, then I could select the target framework from the project dropdown, but I want to avoid having compile-time logic in my files.如果我在每个文件中使用#if NETFRAMEWORK #elif NET6_0 #endif语法,那么我可以从项目下拉列表中选择目标框架,但我想避免在我的文件中包含编译时逻辑。 I could also keep going back to my csproj and updating the tag, but I don't want to do that either in case I accidentally forget to change it back.我也可以继续回到我的 csproj 并更新标签,但我也不想这样做,以防我不小心忘记将其改回。

Is there a good to change the target framework in the IDE when targeting multiple frameworks like this?像这样针对多个框架时,在 IDE 中更改目标框架有好处吗?

Create a Directory.Build.targets file in your solution folder and copy the contents below:在您的解决方案文件夹中创建一个Directory.Build.targets文件并复制以下内容:

<Project>

  <PropertyGroup>
    <BuildDependsOn>
      RemoveNet48Files;
      RemoveNet60Files;
      $(BuildDependsOn)
    </BuildDependsOn>
  </PropertyGroup>

  <Target Name="RemoveNet48Files" Condition="'$(TargetFramework)' != 'net48'">
    <ItemGroup>
      <Compile Remove="NetFramework\**" />
      <EmbeddedResource Remove="NetFramework\**" />
      <None Remove="NetFramework\**" />
    </ItemGroup>
  </Target>

  <Target Name="RemoveNet60Files" Condition="'$(TargetFramework)' != 'net6.0'">
    <ItemGroup>
      <Compile Remove="AspNetCore\**" />
      <EmbeddedResource Remove="AspNetCore\**" />
      <None Remove="AspNetCore\**" />
    </ItemGroup>
  </Target>

</Project>

This way the files will be removed from the item group only before build.这样,文件只会在构建之前从项目组中删除。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM