Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Workaround for C# conditional compilation depending on CLR version

Posted on 2006-06-14
3
Medium Priority
?
1,390 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
[X]
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
3 Comments
 
LVL 6

Assisted Solution

by:neilprice
neilprice earned 375 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 375 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

609 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