Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation depending on the framework version in C#

Tags:

Are there any preprocessor symbols which allow something like

#if CLR_AT_LEAST_3.5 // use ReaderWriterLockSlim #else // use ReaderWriterLock #endif 

or some other way to do this?

like image 576
Alexey Romanov Avatar asked Jan 03 '09 11:01

Alexey Romanov


People also ask

What statements are used for conditional compilation in C?

The compiler directives that are used for conditional compilation are the DEFINE directive, the EVALUATE directive, and the IF directive.

What is conditional compilation in preprocessor?

The preprocessor conditional compilation directive spans several lines: The condition specification line (beginning with #if , #ifdef , or #ifndef ) Lines containing code that the preprocessor passes on to the compiler if the condition evaluates to a nonzero value (optional) The #elif line (optional)

What is #if and #endif in C#?

These are for compiler constants, for example: #if DEBUG Debug.WriteLine("This is written in debug mode"); #endif. If the DEBUG constant is defined, that code gets compiled, if it's not then it's stripped out, ignored by the compiler..it's a way to determine what's in a certain build type, and stripped out for another.


1 Answers

I don't think there are any predefined 'preprocessor' symbols. However you can achieve what you want like this:

  1. Create different configurations of your project, one for every version of CLR you want to support.

  2. Choose a symbol like VERSION2, VERSION3 etc. per CLR version.

  3. In every configuration, define the one symbol associated with it and undefine all others.

  4. Use these symbols in conditional compilation blocks.

like image 86
Frederick The Fool Avatar answered Sep 23 '22 02:09

Frederick The Fool