简体   繁体   中英

How does C# compile the code with #if preprocessor directive?

I wonder how to compiler compile my code if I using #if directive inside my code. I would like to create special version of my application (commercial demo version) and I want to limit the functionality of my application. I would rather avoid the obfuscation and just don't want to add all my compiled code to executable file. I need solution resists preview my code during disassembly process. Can I use #if directives for variant compilation instead making comments for disabling code parts?

Using the #if directive is like using the preprocessor in c++, in that the code would simply not be present if the condition hasn't been met (in compilation time). From MSDN :

When the C# compiler encounters an #if directive, followed eventually by an #endif directive, it will compile the code between the directives only if the specified symbol is defined. Unlike C and C++, you cannot assign a numeric value to a symbol; the #if statement in C# is Boolean and only tests whether the symbol has been defined or not.

作为@ohadsc答案的补充:您始终可以使用Reflector检查编译器实际生成的内容。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM