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
?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With