Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Conditional Compilation in project reference in Visual Studio

I have a class library A that is used in other projects in my solution like B and C.

Class library A behaves differently based on the presence of a pre-processor directive, for example :

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

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

like image 531
Naser Asadi Avatar asked May 14 '13 09:05

Naser Asadi


People also ask

How to 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 conditional compilation in VB net?

Conditional compilation is the ability to specify that a certain block of code will be compiled into the application only under certain conditions. Conditional compilation uses precompiler directives to affect which lines are included in the compilation process.

What is #if debug C#?

The code which is written inside the #if debug will be executed only if the code is running inside the debug mode. If the code is running in the release mode then the #if Debug will be false and it will not execute the code present inside this.


1 Answers

You can do something like this using the ConditionalAttribute

This is how Debug.WriteLine() is present or non-present depending on presence of the "DEBUG" symbol.

You will be able to define your own name for the conditional symbol that you use to control the presence or absence of the code.

You can then put that symbol into the list in the "Conditional compilation symbols" settings on the project properties "Build" tab for the project where you want the code.

This doesn't allow you to have the "some other code" part unfortunately, and also it only applies to entire methods.

like image 59
Matthew Watson Avatar answered Sep 22 '22 14:09

Matthew Watson