C# 中 #if DEBUG 预处理器指令的用法是什么?我们什么时候必须使用这个?
C# 中#if DEBUG
预处理器指令的用法是什么?我们什么时候必须使用这个?
What is the usage of #if DEBUG
pre-processor directive in C#? When must we use this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在调试模式下:
输出
在发布模式下:
输出如下
:#if (C#参考)
用法:如果您有一组值要在调试模式下测试,而不是在发布模式下测试,则可以使用
#if DEBUG
In Debug mode:
Output as
In Release mode:
Output as
read this: #if (C# Reference)
Usage: If you have a set of values to be tested in the debug mode and not in the release mode you can use
#if DEBUG
您可能会对
Conditional
属性感到更舒服,该属性可用于排除整个方法,而无需在调用方使用条件使源代码变得复杂:Foo()
可以在调试和发布模式下都可以安全地调用 - 但是在发布模式下,这将是一个无操作。You might feel more comfortable with the
Conditional
attribute, which can be used to exclude entire methods, without needing to complicate the source with conditionals on the calling side:Foo()
can be safely called in both debug and release - however in release mode, it would be a no-op.您根本不必使用它。其目的是让代码段仅在调试模式下编译。例如,您可能有一些启用主用户的代码,它可以冒充系统中的任何其他用户进行测试和调试。出于安全原因,您不希望在发布代码中启用该用户,因此您应该将代码的相关部分包装在 #if DEBUG 中,并且它们将从发布代码中排除。
You don't have to use it at all. The purpose of it is to have sections of code that only get compiled in debug mode. e.g. you might have some code that enabled a master user, that could pretend to be any other user in the system for testing and debug pruposes. You would not want that user enabled in the release code for security reasons so you waould wrap the relevent sections of code in #if DEBUG and they would be excluded from release code.
编译时,您可以设置编译器标志,您可以使用这些标志将代码放入这些指令中。该代码将不会被编译,并且永远不会出现在最终的汇编输出中。 DEBUG 是预定义的之一,但您可以使用自己的。
作为使用示例:在当前的开发之一中,我们使用编译器标志来声明是否使用登录掩码来登录用户,或者是否应使用当前主体自动进行登录。第二种模式仅适用于开发人员,例如无需登录即可更快地进行调试。
另一个示例:在某些单声道代码中,您将看到标志。在这种情况下,当例如针对不同的框架时,代码可能会被不同地编译,因为它使用早期版本中可能不存在的类。
与此相关的是条件-属性,您可以使用它来标记方法。如果未设置该标志,则不会执行对该方法的调用。该方法仍然在 IL 中结束,但调用将被删除。
检查以下代码示例:
您会注意到 Debug.WriteLine 方法应用了 Conditional 属性:当您在没有 DEBUG 编译器标志的情况下进行编译时,对它的调用将被删除。
When compiling you can set Compiler flags which you can use to put code into those directives. That code will not be compiled and never ends up in the final assembly output. DEBUG is one of the predefined ones, but you can use your own.
As an example of usage: In one of the current developments we use a compiler flag to state whether to use A login mask to log in a user, or whether login should occur automatically with the current principal. The second mode is only for the developers to e.g. debug quicker without having to log in.
Another example: In some mono code you will see flags. In this case code may be compiled differently when e.g. targetting a different framework, as it uses classes that may not exist in earlier releases.
Related to this is the Conditional-Attribute with which you can mark a method. If said flag isn't set, calls to the method will not be performed. The method still ends up in the IL, but calls will be removed.
Check for example following code:
You will notice that the Debug.WriteLine method has the Conditional attribute applied to it: Calls to it will be removed when you compile WITHOUT the DEBUG compiler flag.
在测试 Windows 服务时它也很有用。您可以放入 #if DEBUG 来手动启动进程,这样您就不必安装该服务并附加到它来进行调试。
it also comes in handy when testing windows services. You can put in an #if DEBUG to start your process manually, so you don't have to install the service and attach to it to debug.