The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ Assert

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. namespace System.Diagnostics
  16. {
  17.     using System;
  18.     using System.Security.Permissions;
  19.     using System.IO;
  20.     using System.Reflection;
  21.     using System.Runtime.CompilerServices;
  22.     // Class which handles code asserts. Asserts are used to explicitly protect
  23.     // assumptions made in the code. In general if an assert fails, it indicates
  24.     // a program bug so is immediately called to the attention of the user.
  25.     // Only static data members, does not need to be marked with the serializable attribute
  26.     static internal class Assert
  27.     {
  28.         private static AssertFilter[] ListOfFilters;
  29.         private static int iNumOfFilters;
  30.         private static int iFilterArraySize;
  31.        
  32.         static Assert()
  33.         {
  34.             Assert.AddFilter(new DefaultFilter());
  35.         }
  36.        
  37.         // AddFilter adds a new assert filter. This replaces the current
  38.         // filter, unless the filter returns FailContinue.
  39.         //
  40.         public static void AddFilter(AssertFilter filter)
  41.         {
  42.             if (iFilterArraySize <= iNumOfFilters) {
  43.                 AssertFilter[] newFilterArray = new AssertFilter[iFilterArraySize + 2];
  44.                
  45.                 if (iNumOfFilters > 0)
  46.                     Array.Copy(ListOfFilters, newFilterArray, iNumOfFilters);
  47.                
  48.                 iFilterArraySize += 2;
  49.                
  50.                 ListOfFilters = newFilterArray;
  51.             }
  52.            
  53.             ListOfFilters[iNumOfFilters++] = filter;
  54.         }
  55.        
  56.         // Called when an assertion is being made.
  57.         //
  58.         public static void Check(bool condition, string conditionString, string message)
  59.         {
  60.             if (!condition) {
  61.                 Fail(conditionString, message);
  62.             }
  63.         }
  64.        
  65.        
  66.         public static void Fail(string conditionString, string message)
  67.         {
  68.             // get the stacktrace
  69.             StackTrace st = new StackTrace();
  70.            
  71.             // Run through the list of filters backwards (the last filter in the list
  72.             // is the default filter. So we're guaranteed that there will be atleast
  73.             // one filter to handle the assert.
  74.            
  75.             int iTemp = iNumOfFilters;
  76.             while (iTemp > 0) {
  77.                
  78.                 AssertFilters iResult = ListOfFilters[--iTemp].AssertFailure(conditionString, message, st);
  79.                
  80.                 if (iResult == AssertFilters.FailDebug) {
  81.                     if (Debugger.IsAttached == true)
  82.                         Debugger.Break();
  83.                     else {
  84.                         if (Debugger.Launch() == false) {
  85.                             throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DebuggerLaunchFailed"));
  86.                         }
  87.                     }
  88.                    
  89.                     break;
  90.                 }
  91.                 else if (iResult == AssertFilters.FailTerminate)
  92.                     Environment.Exit(-1);
  93.                 else if (iResult == AssertFilters.FailIgnore)
  94.                     break;
  95.                
  96.                 // If none of the above, it means that the Filter returned FailContinue.
  97.                 // So invoke the next filter.
  98.             }
  99.            
  100.         }
  101.        
  102.         // Called when an assert happens.
  103.         //
  104.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  105.         public static extern int ShowDefaultAssertDialog(string conditionString, string message);
  106.     }
  107. }

Developer Fusion