.NET生产代码中的“声明"语句 [英] 'Assert' statements in .NET production code

查看:72
本文介绍了.NET生产代码中的“声明"语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将""和" Debug.Assert "语句保留在稳定"的代码中并移入测试和生产环境中是否明智?

Is it wise to leave Trace.Assert and Debug.Assert statements in code that is "stable" and which has been moved into Test and Production environments?

如果是,这些断言语句如何提供帮助?有Guard类等检查异常条件并根据情况引发异常是否足够?

If so, how do these assertion statements help? Isn't it sufficient to have Guard classes, etc. check for exception conditions and raise exceptions as appropriate?

推荐答案

调试.除非定义了DEBUG编译常量,否则Assert 语句将被忽略,默认情况下,在您使用"debug"配置而不是"release"配置进行编译时会发生这种情况.实际上,Debug类实际上仅用于测试环境,在该环境中,您应该捕获所有(或至少大多数)会导致Debug.Assert失败的错误.

Debug.Assert statements will be ignored unless you have the DEBUG compilation constant defined, which by default happens when you compile in the "debug" configuration and not in the "release" configuration. Indeed, the Debug class is inteded to be used only in testing environments, where you are supposed to catch all (or at least most) of the bugs that would cause Debug.Assert to fail.

Trace.Assert 的工作方式相同,不同之处在于编译必须存在的常量是TRACE,默认情况下,该常量同时存在于调试"和发布"配置中.在发布代码中包含跟踪声明可能是有意义的,但是通常最好使它们执行除方法的默认行为(该方法仅显示带有堆栈跟踪的消息框)以外的其他操作.您可以通过为Trace类配置自定义跟踪侦听器来实现此目的.有关更多详细信息,请参见方法文档.

Trace.Assert works the same way, except that the compilation constant that must exist is TRACE, which by default exists in both "debug" and "release" configurations. It may make sense to have trace assertions in release code, but it is usually preferable to make them do something else than the default behavior of the method (which just displays a message box with the stack trace). You can achieve this by configuring a custom trace listener for the Trace class; see the method documentation for more details.

这篇关于.NET生产代码中的“声明"语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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