Assert.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FXUpdate3074 / FXUpdate3074 / 1.1 / untmp / whidbey / QFE / ndp / clr / src / BCL / System / Diagnostics / Assert.cs / 1 / Assert.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 
namespace System.Diagnostics {
    using System; 
    using System.Security.Permissions; 
    using System.IO;
    using System.Reflection; 
    using System.Runtime.CompilerServices;
    // Class which handles code assertts.  Asserts are used to explicitly protect
    // assumptions made in the code.  In general if an assertt fails, it indicates
    // a program 
 
    internal static class Assert 
    { 
        private static AssertFilter[] ListOfFilters;
        private static int iNumOfFilters; 
        private static int iFilterArraySize;

        static Assert()
        { 
            Assert.AddFilter(new DefaultFilter());
        } 
 
      // AddFilter adds a new assertt filter. This replaces the current
      // filter, unless the filter returns FailContinue. 
      //
      public static void AddFilter(AssertFilter filter)
      {
         if (iFilterArraySize <= iNumOfFilters) 
         {
               AssertFilter[] newFilterArray = new AssertFilter [iFilterArraySize+2]; 
 
                  if (iNumOfFilters > 0)
                  Array.Copy(ListOfFilters, newFilterArray, iNumOfFilters); 

               iFilterArraySize += 2;

                 ListOfFilters = newFilterArray; 
         }
 
         ListOfFilters [iNumOfFilters++] = filter; 
      }
 
      // Called when an asserttion is being made.
      //
      public static void Check(bool condition, String conditionString, String message)
      { 
         if (!condition)
         { 
            Fail (conditionString, message); 
         }
      } 


      public static void Fail(String conditionString, String message)
      { 
         // get the stacktrace
         StackTrace st = new StackTrace(); 
 
         // Run through the list of filters backwards (the last filter in the list
         // is the default filter. So we're guaranteed that there will be atleast 
         // one filter to handle the assertt.

         int iTemp = iNumOfFilters;
         while (iTemp > 0) 
         {
 
            AssertFilters iResult = ListOfFilters [--iTemp].AssertFailure (conditionString, message, st); 

            if (iResult == AssertFilters.FailDebug) 
            {
               if (Debugger.IsAttached == true)
                  Debugger.Break();
               else 
               {
                  if (Debugger.Launch() == false) 
                  { 
                     throw new InvalidOperationException(
                           Environment.GetResourceString("InvalidOperation_DebuggerLaunchFailed")); 
                  }
               }

               break; 
            }
            else if (iResult == AssertFilters.FailTerminate) 
               Environment.Exit(-1); 
            else if (iResult == AssertFilters.FailIgnore)
               break; 

            // If none of the above, it means that the Filter returned FailContinue.
            // So invoke the next filter.
         } 

      } 
 
      // Called when an assertt happens.
      // 
      [MethodImplAttribute(MethodImplOptions.InternalCall)]
      public extern static int ShowDefaultAssertDialog(String conditionString, String message);
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK