Expert Texture Home Contact me About Subscribe Digipede Connect on LinkedIn rwandering on Twitter rwandering on FriendFeed

rwandering.net

The blogged wandering of Robert W. Anderson

Three deficiencies in the C# preprocessor

Here are three things I want the C# preprocessor to do. Together they would better support cross-compilation for different .NET versions. Of course, this means that I really want these for C# / .NET 1.1. Too late for that?

1. Ignore all text in #if‘ed out code. Ideally this would simply ignore all text between the #if and the matching #else or #endif. You may think it already does this, but of course it does not. It looks at all of the preprocessor commands. Why do I care? Well, I want to use #pragma warning disable around some specific method-calls to members we plan to make obsolete. This doesn’t work before C# 2.0, so I would like to #if out these pragmas (and turn off all such warnings globally in those builds too, by the way):

#if !__DOTNET11__
#pragma warning disable 618
#endif

Of course, this doesn’t work at all because previous versions of C# choke on the warning pragma. It may be too much to call this one a deficiency, but it sure would be nice if it worked.

2. Allow expressions in #if. See the next one for the reason.

3. Provide compiler-version and .NET version constants. This is kind of obvious, but why do I have to define my own constant (as in the above example)? C++ supports the __CLR_VER constant. Why not C#? I would prefer:

#if __CLR_VER < 2.0
. . . 
#endif

Of course, this requires expression evaluation and I’m intentionally blurring the distinction between textual constants and mathematical operators.

There are workarounds to these issues, but handling the warnings properly is kind of thorny. I’m a big believer in clean builds, so I think it is worth the time to implement this, but I sure do wish Microsoft had built this in. My understanding is that cross-compilation was not considered a requirement for VS2005 and MSBuild when it was released — not having this kind of support built into the language is another example of that.

    Trackback

6 Comments »