Changing conditional compilation in a project link in Visual Studio

I have an A class library that is used in other projects of my solution, such as B and C.

The class library A behaves differently depending on the availability of the preprocessor directive, for example:

#if some_directive // some code #else // some other code #end 

How can I use class library A in project B with some_directive enabled, but use in project C with some_directive disabled?

+4
source share
3 answers

It seems that this feature is currently not supported. According to this post :

The language does not support the concept of links through preprocessor macros. What you can do is use the msbuild file and change the set of links added based on the msbuild options.

Another workaround that I used was to use the solution configuration in "Configuration Manager". I created two configurations for building each project B or C, for which the preprocessor directive is included in only one of these configurations.

+1
source

You can do something like this using ConditionalAttribute

This is how Debug.WriteLine() present or not present depending on the presence of the "DEBUG" character.

You can define your own name for the conditional character that you use to control the presence or absence of the code.

Then you can put this symbol in the list in the "Conditional compilation symbols" options on the "Build" tab of the project for the project in which you want to get the code.

This does not allow you, unfortunately, to have part of "other code", and also applies only to all methods.

+1
source

I know that this topic is old - for me the following approach works beautifully and can fit just as well: comments (dis) the advantages of this approach are welcome. If you add your .cs file as an existing element as a link to each project, you can compile it using different directives. To add an existing element as a linked file, see Screenshots in this post Use folders to organize related files.

 // file ClassA.cs namespace HelperClasses { public ClassA { #if some_directive // some code #else // some other code #end // .... } } // using statement in Project B and C using HelperClasses // Add ClassA.cs in both Projects B and C // as exiting, linked File -- not as a Reference // set the compiler-Directives according your needs 
0
source

Source: https://habr.com/ru/post/1480662/


All Articles