[Mono-list] Preprocessor

Jonathan Pryor jonpryor@vt.edu
Tue, 12 Oct 2004 06:15:20 -0400


On Tue, 2004-10-12 at 05:41, Fabien Meghazi wrote:
> Got a simple question and I'm not lucky with google for this.
> I'm searching the preprocessor directive that allows to replace names
> before compilation. I know it is possible in C but I don't remember
> how to, I guess it's not DEFINE.
> My goal is to do this
> 
> DIRECTIVE SERVAR HttpContext.Current.RequestServerVariables.GetValues

C# does not support macros, in any form.  Consequently, this isn't
possible within C#.  It does support conditional compilation via
#if...#endif, but that has nothing to do with macros.

In order to simulate this behavior, you would have to resort to a
separate pre-processor, such as awk, sed, m4, or even the C
Pre-Processor.  These aren't exactly straightforward to use, but it can
be done.

For example, using `sed' you could use SERVAR in your code, and as part
of your Makefile do this:

	cat MySource.cs.in | \
	  sed 's/SERVAR/HttpContext.Current.RequestServerVariables.GetValues/g' \
	  > MySource.cs

Then use MySource.cs when compiling your other source code.

Of course, with something as "blunt" as sed, you have to be particularly
careful about what you substitute, as it will replace SERVAR *anywhere*,
even within strings.  Typical 'sed' convention (as seen on configure
scripts) is to enclose the variable in `@', e.g. @SERVAR@.

 - Jon