分离调试和发布代码在C# [英] Segregating Debug and Release Code in C#

查看:211
本文介绍了分离调试和发布代码在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,其中我有一些的调试代码,我不希望删除,但我希望它被修改或编译发布时删除/发表。例如,我想在调试版本是这样的:

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



我宁愿不必添加/删除的条件编译符号的每一个更改生成类型时项目属性;它应该自动发生。有没有办法在Microsoft Visual C#2008 Express Edition的做到这一点?谢谢

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.

推荐答案

使用:

#if DEBUG
  // Debug work here
#else
  // Release work here
#endif

如果你这样做,只要确保在属性页打开定义DEBUG常数切换(编译项目的属性页),它会工作。这是默认为新的C#项目设置为true。 DEBUG 将获得由C#编译器为你定义(默认情况下)。

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.

这篇关于分离调试和发布代码在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆