C# - 根据CPU平台选择TargetFrameworks

问题描述 投票:0回答:1

我有一个为 x86 编译的旧版 C# 应用程序。我将其升级到 VS 2022,并除了 x86 之外还启用 x64。我想使用 .Net Framework 4.5 编译旧版 x86,使用 .Net 8.0 编译 x64。因此,有效的组合是 x86-.NetFramework4.5 和 x64-.Net6.0。

是否可以根据CPU平台设置TargetFrameworks?

.net .net-core msbuild
1个回答
0
投票

您无法在单个工程文件中直接根据CPU平台设置

TargetFramework

但是,您可以通过在项目中使用多种配置来实现此目的。

x86
文件中为
x64
.csproj
创建单独的配置,并为每个配置指定相应的
TargetFramework

例如:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>YourNamespace</RootNamespace>
    <AssemblyName>YourAssemblyName</AssemblyName>
    <TargetFrameworks>net45;net8.0</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
    <TargetFramework>net45</TargetFramework>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
    <TargetFramework>net45</TargetFramework>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <TargetFramework>net8.0</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <TargetFramework>net8.0</TargetFramework>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

</Project>
  • 构建项目时,选择所需的配置并
    Debug/x86
    中的平台(例如,
    Release/x64
    Visual Studio
    )。
  • 然后
  • Visual Studio
    将使用相应的设置中指定的 地产集团。
© www.soinside.com 2019 - 2024. All rights reserved.