繁体   English   中英

Visual Studio,构建配置:如何进行基于另一个的构建配置

[英]visual studio, build configuration: how to make a build configuration that is based on another

我希望为C#visual studio项目添加一个新的构建配置。 我希望它就像调试构建配置,但有一个区别。 我希望即使调试配置发生更改,它也总是像调试配置一样。

我该怎么做呢?

这是使用不同预处理器定义的示例。 您必须手动编辑项目文件。 我建议您在VS本身中执行此操作,因为它具有语法突出显示和自动完成功能。 在普通的csproj文件中, Debug|AnyCPU配置的属性定义如下(1):

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <PlatformTarget>AnyCPU</PlatformTarget>
  <DebugType>pdbonly</DebugType>
  <Optimize>false</Optimize>
  <OutputPath>bin\Debug\</OutputPath>
  <DefineConstants>DEBUG;TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
</PropertyGroup>

假设您想重用除DefineConstants之外的所有内容,您将创建一个单独的项目文件debug.props仅用于定义公共属性,并将其与项目文件放在同一目录中:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
</Project>

然后,只需调整主项目文件以导入通用文件,然后根据配置设置一些不同的值即可。 这是通过将(1)替换为:

<Import Project="$(MsBuildThisFileDirectory)\debug.props"
   Condition="'$(Configuration)'=='Debug' Or '$(Configuration)'=='MyDebug'" />
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
  <DefineConstants>DEBUG</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'MyDebug|AnyCPU' ">
  <DefineConstants>TRACE;DEBUG</DefineConstants>
</PropertyGroup>

应该很清楚这是做什么的:它将导入具有公共属性的文件(如果配置是Debug或MyDebug),然后根据使用的配置为DefineConstants设置不同的值。 由于现在有一个Configuration == MyDebug的PropertyGroup,VS会自动重新识别它,因此在Configuration Manager中,您现在可以选择MyDebug作为Configuration。 一旦这样做,它将影响如下代码:

#if TRACE //is now only defined for MyDebug config, not for Debug
Console.WriteLine( "hello there" );
#endif

暂无
暂无

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

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