Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define conditional compilation symbols in separate file (not .csproj or app.config)

We need to define a conditional compilation symbol in a class library project. This should not be checked in the source control (it doesn't apply to all developers), so it should be defined in someplace other than the .csproj or the app.config file. How can this be achieved?

like image 591
Edo Avatar asked Aug 03 '09 09:08

Edo


2 Answers

I would define your various build types in the configuration manager (menu BuildConfiguration Manager) and set up each of the required constants on each of the build types.

You can then have each member of the team simply select the build type they want to do, and it will automatically use the appropriate constants. (I think that the most recently used build type is stored in the .suo file, which is "solution user options" and you wouldn't normally check in to your source control, so this would be maintained specifically for each user).

You can define pre-processor constants on the C# compiler command line using the /define switch, but you will have the problem of how to call this. Any changes you make to the project properties to use this will be saved in the .csproj file. You would have to do all of your building from the command line instead which I'm sure you won't want. You can also define them in MSBuild scripts, but you would have the same problem.

like image 147
Simon P Stevens Avatar answered Nov 15 '22 02:11

Simon P Stevens


This works:

It turns out you can define conditional symbols in the csproj.user file. I assume the same thing would work for other languages, but I haven't tried.

So just add something like the following to the csproj.user file.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>MySpecialConstant, TestBlahBlah</DefineConstants>
</PropertyGroup>

I actually like @SimonPStevens solution better because it embraces TFS, instead of hiding. Sometimes, however, this is just easier...

like image 29
EBarr Avatar answered Nov 15 '22 03:11

EBarr