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 proc
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.
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
I faced this some time ago - had to clean up certain outdated C# preprocessor branches in my old project leaving relevant branches intact. The built-it C preprocessor could not do it for me as its output file never contains preprocessor directives. Of course, there are C processor tools such as unifdef or coan that can partially remove outdated branches for C/С++. The thing is that they do not fully support С# syntax - they fail on #region and #pragma, for instance. So, I had to write my own python tool undefine for my task. I used regexp for parsing directives and the sympy library - for simplifying and calculating logical expressions. To resolve your task try the following:
>>python undefine apply -d SUBPROJECT your_project_path
The tool worked fine with my large ~10m lines codebase.
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...
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?