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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With