Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Conditional compilation symbols be added to csproj.user file?

I'm working in VS 2013 with a C# Xamarin iOS project. I would like to add a Conditional compilation symbol without effecting anyone else or having to go into Configuration Manager and say copying Debug (primarily so that if someone modifies Debug I don't miss the change).

I've read a few posts stating to try adding something like this to the csproj.user file ...

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>$(DefineConstants);__MY_NEW_SYMBOL__</DefineConstants>
</PropertyGroup>

... but this just removes all the other symbols for the project.

Is there a way I can modify the csproj.user file to achieve this?

like image 701
Gavin Sutherland Avatar asked Feb 28 '14 16:02

Gavin Sutherland


People also ask

How do you use conditional compilation symbols in C#?

Right click the project (The Class Library is a project) and click Properties to open the properties window. Then click the Build tab, input symbols in the “Conditional compilation symbols:” textbox. Use comma to separate each symbol (e.g. “MySymbol1,MySymbol2”). Then use them in this way.

What is Csproj user file?

csproj" is a Visual Studio . NET C# Project file extension. This file will have information about the files included in that project, assemblies used in that project, project GUID and project version etc. This file is related to your project. It will be automatically generated when we create .


1 Answers

I see this is a really old question. I'm not sure if anyone is actually using VS 2013 anymore, but it works in VS2017, exactly the way it's done in the question.

But! I had to run Build -> Clean Solution first before it worked. 'Rebuild Solution' didn't even do it. I had to Clean first, then build and run it.

I tested it with this code:

#if DEBUG
    Console.WriteLine("DEBUG");;
#endif
#if TRACE
    Console.WriteLine("TRACE");
#endif
#if __MY_NEW_SYMBOL__
    Console.WriteLine("__MY_NEW_SYMBOL__");
#endif

Even though my .user file only defines __MY_NEW_SYMBOL__, I saw all three in the console after running it.

My .csproj file has this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

And my .csproj.user file has this:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DefineConstants>$(DefineConstants);__MY_NEW_SYMBOL__</DefineConstants>
</PropertyGroup>
like image 137
Gabriel Luci Avatar answered Oct 20 '22 00:10

Gabriel Luci