Solved

Workaround for C# conditional compilation depending on CLR version

Posted on 2006-06-14
3
1,371 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now