在 C# 中分离调试和发布代码
我正在编写一个应用程序,其中有一些我不想删除的调试代码,但我希望在编译发布/发布时对其进行修改或删除。例如,我希望在调试版本中进行类似的操作:
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
...在发布版本中变为这样:
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
理想情况下,我希望做这样的事情:
#if DEBUG_BUILD
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
我宁愿不必添加/删除 每次更改构建类型时,项目属性中的条件编译符号;它应该自动发生。有没有办法在 Microsoft Visual C# 2008 Express Edition 中执行此操作?谢谢。
I'm writing an application wherein I have some debug code that I do not wish to delete, but I wish it to be modified or removed when compiling for release/publish. For example, I would like something like this in a debug build:
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
...to become this in a release build:
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Ideally, I was hoping to do something like this:
#if DEBUG_BUILD
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
#endif
I would prefer to not have to add/remove a Conditional Compilation Symbol in the project properties every time I change the build type; it should happen automatically. Is there a way to do this in Microsoft Visual C# 2008 Express Edition? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用:
如果您这样做,只需确保打开属性页(项目属性的构建页面)中的“定义调试常量”开关,它就会起作用。对于新的 C# 项目,此值默认设置为 true。
DEBUG
将由 C# 编译器为您定义(默认情况下)。Use:
If you do that, just make sure to turn on the "Define DEBUG Constant" toggle in the property pages (Build page of the Project's properties), and it will work. This is set to true by default for new C# projects.
DEBUG
will get defined for you (by default) by the C# compiler.您也可以使用此属性。
与预处理器指令相比,这有几个优点。
如果未定义条件符号,则对标记为条件的方法的所有调用都将替换为 Nops,这样您就不必修改对其的所有调用。
即使未定义符号,您的代码也会检查错误。 (与使用
#if DEBUG
不同,它在编译期间忽略#else
中的代码)You can also use this attribute.
This has a couple of advantages over the preprocessor directive.
All calls to methods marked conditional will be replaced with Nops if the conditional symbol isn't defined, which saves you having to modify all calls to it.
Your code will checked for errors even when the symbol is not defined. (Unlike when using
#if DEBUG
, which ignores the code in#else
during compilation)您可以使用一个类来编写调试语句
命名空间:系统.诊断
Debug.Assert 是您想要使用的
http:// /msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert.aspx
另请查看 Debug 类以进行所有调试:
http://msdn.microsoft.com/en-us/library/6x31ezs1。 ASPX
There is a class you can use to write your debug statements
namespace: system.diagnostics
Debug.Assert is what you want to use
http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert.aspx
Also look at the Debug class for all debugging:
http://msdn.microsoft.com/en-us/library/6x31ezs1.aspx
您可以编写一个包含条件的扩展方法,这样您就不需要每次都重复它
这就是我的处理方式
然后您的代码行将是
并且 DEBUG 应该已经定义
You could write an extension method that contains the conditional so you don't need to keep duplicating it every time
That would be how I'd go about it
And then your line of code would be
And DEBUG should be defined already
我不知道 Express 版本是否有这个,但 Visual Studio 已经在 C# 中内置了这个。
在你的工具栏中,你肯定有一个下拉菜单,让你(默认情况下)在调试和发布版本之间进行选择,调试版本定义了 DEBUG 符号,因此你可以使用:
I do not know if the express editions have this, but Visual Studio has this already built-in for you in C#.
In your toolbar you surely have the dropdown that let's you (by default) choose between debug and release build, the debug build defines the DEBUG symbol so you can use: