Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Is it possible to conditionally exclude some files from compilation?

I would like to conditionally compile a project excluding specific classes. Is it possible?

UPDATE:

Basically what I'm looking for is to decrease the size of resulting .xap file through command-line directive by not compiling in specific classes (stored in separate .cs files) and all their dependencies.

Here is how MSDN recommends to do it manually. If there was a way to do it conditionally in an automated manner, that'd be a perfect solution.

like image 667
jayarjo Avatar asked Jan 05 '13 20:01

jayarjo


3 Answers

Project file ProjectName.cproj is a plain xml file containing project properties and compiler instructions. Files to include are listed between <ItemGroup>...</ItemGroup> tags. There can be one or more such <ItemGroup> listings. So everything you have to do is put files that you want to be compiled in conditionally, into a separate <ItemGroup> and add a condition as attribute:

<ItemGroup Condition=" '$(BUILD)' == 'IMAGE' ">
    <Compile Include="PngEncoder\Adler32.cs" />
    <Compile Include="PngEncoder\CRC32.cs" />
    <Compile Include="PngEncoder\Deflater.cs" />
    <Compile Include="PngEncoder\DeflaterConstants.cs" />
    <Compile Include="PngEncoder\DeflaterEngine.cs" />
    <Compile Include="PngEncoder\DeflaterHuffman.cs" />
    <Compile Include="PngEncoder\DeflaterOutputStream.cs" />
    <Compile Include="PngEncoder\DeflaterPending.cs" />
    <Compile Include="PngEncoder\IChecksum.cs" />
    <Compile Include="PngEncoder\PendingBuffer.cs" />
    <Compile Include="PngEncoder\PngEncoder.cs" />
</ItemGroup>

Now this group of files will be included only if there is a property defined with the name of BUILD and the value of "IMAGE". Properties can be defined in the project file itself:

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>8.0.50727</ProductVersion>
    ...
</PropertyGroup>

Or passed in from command-line:

msbuild ProjectName.cproj /p:BUILD=IMAGE

msbuild.exe comes with .NET Framework.

like image 138
jayarjo Avatar answered Sep 21 '22 10:09

jayarjo


On builds with Visual Studio Online condition attributes are ignored in ItemGroup elements.

As described here, using When/Choose/Otherwise attributes are supported.

 <Choose>
     <When Condition="'$(Configuration)' == 'Debug With Project References'">
       <ItemGroup>
        <ProjectReference Include="..\SomeProject\SomeProject.csproj">
        <Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project>
        <Name>SomeProject</Name>
      </ProjectReference>
    </ItemGroup>
  </When>
      <Otherwise>
        <ItemGroup>
           <Reference Include="SomeProject">
             <HintPath>..\Libraries\SomeProject.dll</HintPath>
           </Reference>
         </ItemGroup>
       </Otherwise>
    </Choose>
like image 39
Kerem Demirer Avatar answered Sep 19 '22 10:09

Kerem Demirer


You can use the ConditionalAttribute for this:

Indicates to compilers that a method call or attribute should be ignored unless a specified conditional compilation symbol is defined.

[Conditional("SomeCondition")]
public void WillCompileOnlyIfSomeConditionIsDefined()
{
}

An alternative is to use preprocessor directives:

#if !SomeCondition
  // will only compile if SomeCondition is false
#endif
like image 36
Oded Avatar answered Sep 20 '22 10:09

Oded