Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Workaround for C# conditional compilation depending on CLR version

Posted on 2006-06-14
3
1,375 Views
Last Modified: 2012-08-14
I have C# code that needs compiling in both VS .NET 2003 and VS .NET 2005. Turns out the latter defaults to .NET 2.0, and a few tweaks are needed to my code when building in this environment.
I understand C++ supports this by conditional compilation via the predefined __CLR_VER macro, but C# does not. What is the best workaround without forking code and setting up two seperate projects?
0
Comment
Question by:bgauweiler
3 Comments
 
LVL 6

Assisted Solution

by:neilprice
neilprice earned 125 total points
ID: 16907252
Hi,

You have some preprocessor-like support in C# using #DEFINE, #UNDEF, #IF, #ELIF, #ELSE, #ENDIF

(Note that I say preprocessor-like because strictly speaking there is no preprocessor in C#, the preprocessing directives are processed as part of the lexical analysis phase of the compiler)

You'll have to define your own symbols though and change them for each version.

So at the top of your source file you can define like this;

#DEFINE DOTNETTWO // This line when you are compiling against VS2005

then in code

#IF DOTNETTWO
// stuff that's .NET2.0 specific
#ELSE
// stuff that's not
#ENDIF

You can also set project wide symbols through a project's properties to avoid having to type #DEFINE DOTNETTWO at the top of every source file (which could get annoying).  In VS2005 go to properties->Build and add to the CONDITIONAL COMPILATION SYMBOLS box.

Hope this helps
Neil

0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 16909177
You can create different configurations for different .NET versions. For example, instead of standard Debug and Release you can define Debug 2.0, Release 2.0, Debug 1.1 and Release 1.1 configurations. DOTNETTWO constant should be defined in the configuration properties: see Project - Properties - Build - Conditional compilation symbols. Define it only for 2.0 configurations, and use in the code as neilprice suggests.
0
 
LVL 2

Author Comment

by:bgauweiler
ID: 16909653
Thanks guys - I guess you're right, the combination of your answers is probably the easiest way out of the dilemma. Oh, I wished Microsoft had provided a predefined version macro, but I gather the __CLR_VER macro is only available in VS2005, and only for C++. Grrr.
Thanks for your help.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question