views:

392

answers:

4

Hi,

Does anyone know of a utility to preprocess a C# source file without compiling it, in a similar fashion to using the -E flag in GCC? I tried using GCC - it successfully processes #if directives, but it chokes on any #region directives...

Ideally, I'd like to be able to run the tool over a .cs file to remove any #if blocks that evaluate to false, and optionally be able to flag whether to remove/leave intact comments, #region, #pragma directives etc.

To put this in context, I want to be able to a publish some source code that is part of a (much) larger project, whilst removing portions that are only relevant to the larger project. As an example, there are chunks of code that look like this:

#if (SUBPROJECT)
namespace SubProject
#else
namespace CompleteProject
#endif
{
  public class SomeClass()
  {
#if (!SUBPROJECT)
    // This might call a method contained in an assembly that potentially 
    // won't be available to the sub-project, or maybe a method that hooks
    // into a C library via P/Invoke...
    string result = CallSomeCleverMethod();
#else
    // This might call a method that performs a simplified version of the 
    // above, but does so without the above's assembly or P/Invoke 
    // dependencies...
    string result = CallSomeSimpleMethod();
#endif
  }
}

Please remember I'm not trying to do builds here - this is purely about publishing only a subset of a larger project's source code.

Any ideas/help appreciated.

A: 

Might it not be easier, and more idiomatically C#, to create interfaces, and have different implementations of those interfaces? One set that you can package up a as a library and redistribute?

Brian Sullivan
I need to be able to process actual class/namespace names, metadata (custom attributes etc) - see the namespace example in the question. I also don't want to add implementation complexity where it isn't otherwise needed.
Mark Beaton
You don't think the above code is already complex? I still think you're really coming at this from a C++ mindset and maybe missing some tools that C# offers, but best of luck finding a solution that will make you happy!
Brian Sullivan
+1  A: 

I'm not aware of a preprocessor for C# that would allow you to export a subset of your source code without building it.

However, You should be able to achieve the same effect by using a code generation template. Just change to the preprocesor directives to the appropriate template equivalent.

Visual Studio 2008 has a built-in code generation tool called T4. There are other third-party options like CodesmithTools or the free MyGeneration.Net

ichiban
Mark Beaton
+2  A: 

I do not believe that there are any flags for the csc or msc (mono compiler) to output the preprocessed files.

If I had to do this, and it was a small subset I would compile and then use Reflector to decompile back to C# and export the files. If you have a lot of files, there is an Reflector Add-In called FileGenerator for Reflector.

Unfortunately this will not be a true copy of your codebase -- it will reformat all of the code and you will lose all of the comments.

Jack Bolding
+1  A: 

It turns out using the GNU C preprocessor (cpp) directly (rather than via gcc) provides more control over the output. Using this, I was able to process the source files in a suitable fashion...

Here's an example:

cpp -C -P -DSUBPROJECT Input.cs Output.cs

I tried to get the C/C++ compiler provided with Visual Studio to do something similar, but gave up after a while...

Mark Beaton