Segregating Debug and Release Code in C#

落爺英雄遲暮 提交于 2019-12-30 08:09:07

问题


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.


回答1:


Use:

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

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.




回答2:


You can also use this attribute.

[Conditional("DEBUG")]

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)




回答3:


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




回答4:


You could write an extension method that contains the conditional so you don't need to keep duplicating it every time

public static class ExceptionExtensions
{
    public static String ToMyString(this Exception ex)
    {
#if DEBUG
        return ex.ToString();
#else
        return ex.Message;
#endif
    }
}

That would be how I'd go about it

And then your line of code would be

MessageBox.Show(ex.ToMyString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

And DEBUG should be defined already




回答5:


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:

#ifdef DEBUG
    // debug code
#endif


来源:https://stackoverflow.com/questions/3311390/segregating-debug-and-release-code-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!