简体   繁体   English

C#如何使用#if预处理程序指令编译代码?

[英]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. 我想知道如果我在代码中使用#if指令,如何编译我的代码。 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? 我可以使用#if指令进行变体编译,而不是通过注释来禁用代码部分吗?

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). 使用#if指令就像在c ++中使用预处理器一样,因为如果不满足条件(在编译时),该代码将根本不存在。 From MSDN : 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. 当C#编译器遇到#if指令,最后遇到#endif指令时,仅在定义了指定符号的情况下,编译器才会在指令之间编译代码。 Unlike C and C++, you cannot assign a numeric value to a symbol; 与C和C ++不同,不能将数字值分配给符号。 the #if statement in C# is Boolean and only tests whether the symbol has been defined or not. C#中的#if语句为布尔型,仅测试符号是否已定义。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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