Conditionally exclude from Release build

N

Nevyn Twyll

I have a bunch of test case classes (.cs) files in my executable.
For security and bloat reasons, I don't want them to compile into a Release
executable - only into a Debug build.

How do I conditionally exclude files or classes from my Release builds?

In old C++ we would use #ifdef ... #endif statements, but we don't have
pragmas in C# (or don't seem to).

Help?
 
S

Sherif ElMetainy

Hello

Yes you have

#if DEBUG
// your debug code here
#else
// your release code here
#endif

Best regards,
Sherif
 
G

Guest

did you look hard enough? :)

there's #if #else #elif #endif #define #undef directives you can use in C#.

hope that helps.
 
K

Kai Brinkmann [MSFT]

The previous posters pointed out the existence of preprocessor directives in
C#.

However, you can also make individual methods conditional by using the
Conditional attribute, e.g.

[Conditional("Debug")]
public void foo() {
// some code
}

There are a few conditions however:

1. The method must be in a struct or class declaration (not an interface)
2. The return type must be void
3. You must not use the override modifier (virtual is OK)
4. The conditional method must not be an implementation of an interface
method.

Hope that helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top