views:

100

answers:

3

Hi there,

I was wondering if there is a way to conditionally compile entire namespaces in C#. Or am I left with having to explicitly decorate each source file within the namespace with the preprocessor directives to exclude it? In sub-versions of my application the code in various namespace is simply not required and I would like it excluded.

Thanks in advance!

+3  A: 

If your namespace is in a separate assembly which doesn't contain anything else you can use the Configuration Manager for your specific sub-version and untick the "Build" check box.

If you've got other classes in the assembly though they will not be built or included obviously, and then the only way would be to decorate with pre-processor declarations.

Michael Shimmins
+1  A: 

You would need to put the conditional compilation directive in each file. There is no way to mark an entire namespace as conditionally compiled.

As Michael notes in his answer, a possible solution is to break out the conditional code into a separate project (assembly), and ship that assembly only for configurations that require it; but this will depend on the nature of the conditional code.

itowlson
Thank you for this additional clarification.
Filip K
+1  A: 

You can do this by decorating each file, or you could do it by choosing which files to include. Both MSBuild and csc have options for including all files under a path, and MSBuild additionally has the ability to conditionally include build items based on an attribute (rather than requiring a separate csproj per configuration).

But it is probably easier to decorate the files directly ;-p

Marc Gravell