开发者

Different build action for debug and release builds in Visual Studio

I have this C# Visual Studio solution that implements a plugin architecture. When building a debug build, I want a couple of plugins to be compiled into the main application. When building a rele开发者_开发知识库ase build, I want these .cs-files to be left alone.

Is there a way I can specify different Build Actions for the plugin files for debug and release builds? I would like it to be set to "Compile" in a debug build and to "None" in a release build.


To do this, you'll need to change the .csproj manually, and add a Condition XML Attribute in the Compile XML element corresponding to the file you want only in debug builds. Like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
      ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
      ...
  </PropertyGroup>
  <ItemGroup>
    <!-- Add the Condition attribute here, on each of your debug only files
    Pick up one of the configuration definition configuration above -->
    <Compile Include="DebugOnlyClass1.cs" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "/>
    <!-- normal compilation -->
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
     ...
</Project>


Since plug-ins are usually in separate assemblies, I would create a separate project for each and every plug-in.

For projects you can define whether to build in a configuration or not: In Visual Studio open the Configuration Manager (Menu Build). In the upper left combobox select the Release or Debug configuration and set the build checkboxes in the list ad libitum.

Edit: Otherwise you have to edit the .csproj file manually and add a condition attribute to the item you want to exclude:

<Compile Condition="'$(Configuration)' == 'Debug'" Include="Program.cs" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜