internal static void Assert(bool condition, string whyThisShouldNeverHappen, string detailMessage) { if (!condition) { if (ThrowInsteadOfAssert) { AssertException exception = new AssertException("ASSERT: " + whyThisShouldNeverHappen + " " + detailMessage + " "); throw exception; } Debug.Fail(whyThisShouldNeverHappen, detailMessage); } }
Assert( #if RESHARPER_ATTRIBUTES [JetBrains.Annotations.AssertionCondition(JetBrains.Annotations.AssertionConditionType.IS_TRUE)] #endif bool condition, string whyThisShouldNeverHappen, string detailMessage) { // Early out avoids some slower code below (mostly the locking done in ThrowInsteadOfAssert). if (condition) return; #if ASSERTIONS_TRACE if (!condition) { if (Diagnostics.ThrowInsteadOfAssert) { string assertionMessage = "ASSERT: " + whyThisShouldNeverHappen + " " + detailMessage + " "; AssertException e = new AssertException(assertionMessage); tracer.TraceException(e); throw e; } StringBuilder builder = new StringBuilder(); builder.Append("ASSERT: "); builder.Append(whyThisShouldNeverHappen); builder.Append("."); if (detailMessage.Length != 0) { builder.Append(detailMessage); builder.Append("."); } // 2 to skip this method and Diagnostics.StackTace builder.Append(Diagnostics.StackTrace(2)); tracer.TraceError(builder.ToString()); } #elif CORECLR // Debug.Fail is not in CoreCLR string assertionMessage = "ASSERT: " + whyThisShouldNeverHappen + " " + detailMessage + " "; throw new AssertException(assertionMessage); #else if (Diagnostics.ThrowInsteadOfAssert) { string assertionMessage = "ASSERT: " + whyThisShouldNeverHappen + " " + detailMessage + " "; throw new AssertException(assertionMessage); } System.Diagnostics.Debug.Fail(whyThisShouldNeverHappen, detailMessage); #endif }