4

In VS2017, I had several different build configurations that built an application in different ways. One configuration would produce the default application. Another build configuration would produce the application with more features, etc.

This was done in the source code with #if FEATURE blocks. FEATURE was defined in the Conditional compilation symbols for a project's build configuration.

Now, I ported the code to Visual Studio 2022. It appears that the Conditional compilation symbols are now part of the project and not part of the build configuration. So I have to define FEATURE for the project and not the build configuration.

I've used #if FEATURE to put in attributes to classes and methods, so I can't replace this with a simple if statement in the source code.

I don't want to change the project settings every time I need to build the different applications.

What is the workaround for being able to build a project with different compilation symbols easily?

Robert Deml
  • 12,390
  • 20
  • 65
  • 92
  • Can you give more details about what "I ported the code to Visual Studio 2022" entailed? What changes did you make? In most cases, a VS2017 project should open in VS2022 unless something else changed as part of the migration too. – Jimmy Feb 03 '22 at 19:18
  • 1
    I think I'm going to close this question. The port from VS2017 to VS2022 also included porting the code from .net framework 4.8 to .net 6.0. This messed up a lot of the build configurations. The end result is that VS2022 does support conditional compilation symbols per build configuration, but it was not working because of MANY other failures of porting to .net 6.0. – Robert Deml Feb 03 '22 at 19:24

1 Answers1

1

Realise this is a year old now, but I've been looking at conditional compilation symbols this morning.

First, have a look at this: https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/compiler-options/language

The <DefineConstants> section deals with conditional compilation.

Edit the .csproj file directly, and in any applicable property group you can define constants that you can reference in code. e.g.

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <DefineConstants>MYDEBUGCONSTANT</DefineConstants>
</PropertyGroup>

Then in code you can use:

#if MYDEBUGCONSTANT
    // Some debug code
#endif

I had some issues with the conditions on the property groups, VS2022 got a little confused when two of the property groups applied at the same time. Once I sorted that everything worked as expected.

Hope that helps?

David Brunning
  • 575
  • 7
  • 18