asp:Feature
LANGUAGES:
C#
ASP.NET
VERSIONS: 2.0
Conditional Compilation in ASP.NET 2.0
Include or Exclude Portions of Code from the Compilation
Unit
By Joydip Kanjilal
Conditional compilation dates back to the good old days of
the C language. We could conditionally compile a program written in C, i.e.,
using a pre-defined constant. ASP.NET 2.0 comes with the conditional
compilation feature that enables you to compile a specific portion of your code
using some pre-defined conditional compilation directives, while excluding the
rest of the code from the compilation unit. This article discusses how you can use
this feature, and provides code examples for better understanding.
Conditional compilation is implemented using string
constants in our code. These are also known as symbols. We need to define these
constants either at the page or at the application level and then use them in
our code. This section discusses how we can use this feature of ASP.NET in our
applications.
Let s consider that we require a C# method to compile
conditionally based on the target platform; i.e., a portion of the method would
compile if the target is the Mono Framework, while the other would compile if
the target is the Microsoft .NET Framework. The following code snippet illustrates
how we can use constants in our code to achieve this:
public void SampleMethod ()
{
#if MONO_Framework
//Code targetted at the
MONO Framework
#endif
//Code targetted at the
Microsoft.NET Framework
}
Note that if we want to compile the above method in the
Mono platform, we should define this constant. On the other hand, if we want
our code to be compiled in the Microsoft .NET Framework, we would not define
this constant. Note that if this constant is not defined, the
if construct in the method shown above becomes false and, hence, any
code within it would not be considered for compilation.
Conditional compilation can be done in two ways; i.e.,
either at the page level in the Page directive or at the application level in
the web.config file to make this constant globally accessible by all the pages
of the application.
To define at the page level, use the following snippet:
<%@ Page Language="C#" ...
CompilerOptions="/d:MONO" %>
The /d switch as shown in the above code snippet is used
to define a symbol. To define the same at the application level, use the
following snippet in the web.config file:
<system.codedom>
<compilers>
<compiler
language="c#;cs;csharp" extension=
".cs"
compilerOptions="/d:MONO"
type="Microsoft.CSharp.CSharpCodeProvider,
System,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
</compilers>
</system.codedom>
Conditional compilation is a new feature of ASP.NET. This
article has discussed what conditional compilation is and how we can use this
feature in ASP.NET 2.0 applications. I hope you find this article useful and I
welcome comments and suggestions.
Working extensively in Microsoft technologies for more than 10
years, Joydip Kanjilal is a Senior
Technical Leader in the Design and Architecture team for a company in Hyderabad,
India. His programming
skills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. An ASP.NET
MVP, he has worked with .NET and C# for more than five years. Reach Joydip at mailto:joydipkanjilal@yahoo.com or
at his blog at: http://aspadvice.com/blogs/joydip/.