Solved

Workaround for C# conditional compilation depending on CLR version

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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