conditional compilation - Rudolf Henning

conditional compilation

Posted Monday, April 04, 2005 12:23 PM by rudolf

Rockford Lhotka wrote about an obscure feature in .Net where u can implement a sort of conditional compilation by using attributes. It could be useful in some cases but he points out that its there might be cases where it can lead to all kinds of confusion.

Basically u use the attribute '[Conditional()] e.g.

[Conditional(“MyFlag“) ]
public void Foo()
{
  //stuff here
}

If u compile the code with the above mention method it will (as Rocky puts it) magically dissapear from the IL generated. To have it compiled u need to add the following:

#Const MyFlag = True

Now the method will be compiled into the final IL.

Interesting hey?

Read more here

Filed under:

Comments

# Re: conditional compilation

Monday, April 04, 2005 2:12 PM by Craig Nicholson

Now if only the Microsoft .NET team would implement a constant for the compiler and/or environment so that you can out-of-the-box conditionally build code for the Compact Framework 1.0 and .NET Frameworks 1.0, 1.1 and 2.0. Framework and platform specific workaround/optimisations would be a breeze then.

# re: conditional compilation

Monday, April 04, 2005 2:58 PM by Rudolf

Good idea... hopefully somebody (rightbody) is listening/reading!