Jim*_*ell 11 c# debugging release
我正在编写一个应用程序,其中我有一些我不想删除的调试代码,但我希望在编译发布/发布时修改或删除它.例如,我想在调试版本中使用这样的东西:
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Run Code Online (Sandbox Code Playgroud)
...在发布版本中成为这个:
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望做这样的事情:
#if DEBUG_BUILD
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
Run Code Online (Sandbox Code Playgroud)
我希望每次更改构建类型时都不必在项目属性中添加/删除条件编译符号 ; 它应该自动发生.有没有办法在Microsoft Visual C#2008 Express Edition中执行此操作?谢谢.
Ree*_*sey 15
使用:
#if DEBUG
// Debug work here
#else
// Release work here
#endif
Run Code Online (Sandbox Code Playgroud)
如果你这样做,只需确保在属性页面(项目属性的构建页面)中打开"定义DEBUG常量"切换,它就会起作用.对于新的C#项目,默认情况下设置为true. DEBUG将由C#编译器为您(默认情况下)定义.
Mar*_*k H 14
您也可以使用此属性.
[Conditional("DEBUG")]
Run Code Online (Sandbox Code Playgroud)
这比预处理器指令有几个优点.
如果未定义条件符号,则对标记为条件的方法的所有调用都将替换为Nops,这样您就不必修改对它的所有调用.
即使未定义符号,您的代码也会检查错误.(与使用时不同#if DEBUG,#else在编译期间忽略代码)