Conditional Compilation in ASP.NET 2.0
Include or Exclude Portions of Code from the Compilation Unit
October 30, 2009
asp:Feature
LANGUAGES:C#
ASP.NETVERSIONS: 2.0
Conditional Compilation in ASP.NET 2.0
Include or Exclude Portions of Code from the CompilationUnit
By Joydip Kanjilal
Conditional compilation dates back to the good old days ofthe 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 conditionalcompilation feature that enables you to compile a specific portion of your codeusing some pre-defined conditional compilation directives, while excluding therest of the code from the compilation unit. This article discusses how you can usethis feature, and provides code examples for better understanding.
Conditional compilation is implemented using stringconstants in our code. These are also known as symbols. We need to define theseconstants either at the page or at the application level and then use them inour code. This section discusses how we can use this feature of ASP.NET in ourapplications.
Let s consider that we require a C# method to compileconditionally based on the target platform; i.e., a portion of the method wouldcompile if the target is the Mono Framework, while the other would compile ifthe target is the Microsoft .NET Framework. The following code snippet illustrateshow we can use constants in our code to achieve this:
public void SampleMethod ()
{
#if MONO_Framework
//Code targetted at theMONO Framework
#endif
//Code targetted at theMicrosoft.NET Framework
}
Note that if we want to compile the above method in theMono platform, we should define this constant. On the other hand, if we wantour code to be compiled in the Microsoft .NET Framework, we would not definethis constant. Note that if this constant is not defined, theif construct in the method shown above becomes false and, hence, anycode 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 inthe web.config file to make this constant globally accessible by all the pagesof 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 usedto define a symbol. To define the same at the application level, use thefollowing snippet in the web.config file:
".cs"compilerOptions="/d:MONO" type="Microsoft.CSharp.CSharpCodeProvider, System,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> Conditional compilation is a new feature of ASP.NET. Thisarticle has discussed what conditional compilation is and how we can use thisfeature in ASP.NET 2.0 applications. I hope you find this article useful and Iwelcome comments and suggestions. Working extensively in Microsoft technologies for more than 10years, Joydip Kanjilal is a SeniorTechnical Leader in the Design and Architecture team for a company in Hyderabad, India. His programmingskills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. An ASP.NETMVP, he has worked with .NET and C# for more than five years. Reach Joydip at mailto:[email protected] orat his blog at: http://aspadvice.com/blogs/joydip/.
About the Author
You May Also Like