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.
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.
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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With