Conditional compilation Constant for CLR version

  • Thread starter Thread starter RC
  • Start date Start date
R

RC

Is there a built-in constant that I can use in a #if statement to
conditonally compile a line of code based on the CLR/Compiler version
e.g. something like:

#if (CLRV2)
string Foo =
System.Configuration.ConfigurationManager.AppSettings("..
#else
string Foo =
System.Configuration.ConfigurationSettings.AppSettings("...
 
#if statements are processed before compilation, and long before your
code is actually run (which is when you will get to find out which
version of the CLR is present).

It is possible to check CLR version at runtime using
System.Environment.Version, and you could possibly use this to
conditionally select code at runtime. Something like

(untested)

ISettings settings;
if (Environment.Version.Major >= 2)
settings = (ISettings) new SettingsV2(); //access settings through
ConfigurationManager
else
settings = (ISettings) new SettingsV1(); //access settings through
ConfigurationSettings
settings.doSomething();
 
Back
Top