请考虑以下代码:
#if DEBUG
if (Systems.Contains(system))
throw new InvalidOperationException("System already registered");
#endif
Debug.Assert(!Systems.Contains(system), "System already registered");
以前我曾经做过前者,我后来发现Debug.Assert
。
我不应该总是喜欢Debug.Assert
吗?
[Conditional("DEBUG")]
)。答案 0 :(得分:4)
您始终可以使用Debug.Assert(),因为此类也符合DEBUG条件:
[Conditional("Debug")]
向编译器指示方法调用或属性应该是 除非定义了指定的条件编译符号否则忽略。
ConditionalAttribute应用于中定义的方法 调试和跟踪类。
http://msdn.microsoft.com/en-us/library/system.diagnostics.conditionalattribute.aspx