コード例 #1
0
 public static void Requires(
     bool condition,
     string userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0)
 {
     if (!condition)
     {
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Precondition, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #2
0
ファイル: Contract.cs プロジェクト: sharwell/RuntimeContracts
 public static void RequiresForAll <T>(
     IEnumerable <T> collection,
     Predicate <T> predicate,
     string userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0)
 {
     if (!CheckForAll(collection, predicate))
     {
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Precondition, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #3
0
 public static void AssertNotNull <T>(
     [NotNull] T?value,
     string?userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0) where T : class
 {
     if (value == null)
     {
         userMessage ??= "The value should not be null.";
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Assert, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #4
0
 public static void RequiresNotNull <T>(
     [NotNull] T?o,
     string?userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0) where T : struct
 {
     if (o == null)
     {
         userMessage ??= "The value should not be null.";
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Precondition, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #5
0
 public static void AssertNotNullOrEmpty(
     [NotNull] string?o,
     string?userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0)
 {
     if (string.IsNullOrEmpty(o))
     {
         userMessage ??= "The value should not be null or empty.";
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Assert, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #6
0
 public static void RequiresNotNullOrWhiteSpace(
     [NotNull] string?o,
     string?userMessage                = null,
     [CallerFilePath] string path      = "",
     [CallerLineNumber] int lineNumber = 0)
 {
     if (string.IsNullOrWhiteSpace(o))
     {
         userMessage ??= "The value should not be null or whitespace.";
         ContractRuntimeHelper.ReportFailure(ContractFailureKind.Precondition, userMessage, null, new Provenance(path, lineNumber));
     }
 }
コード例 #7
0
        public static void AssertNotNullOrEmpty(
            [NotNull] string?o,
            string?userMessage                = null,
            [CallerFilePath] string path      = "",
            [CallerLineNumber] int lineNumber = 0)
        {
            if (string.IsNullOrEmpty(o))
            {
                userMessage ??= "The value should not be null or empty.";
                ContractRuntimeHelper.ReportFailure(ContractFailureKind.Assert, userMessage, null, new Provenance(path, lineNumber));
            }
#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
        }
コード例 #8
0
        public static void Invariant(
            bool condition,
#if NETSTANDARD2_0
            [Localizable(false)]
#endif
            string userMessage                = null,
            [CallerFilePath] string path      = "",
            [CallerLineNumber] int lineNumber = 0)
        {
            if (!condition)
            {
                ContractRuntimeHelper.ReportFailure(ContractFailureKind.Invariant, userMessage, null, new Provenance(path, lineNumber));
            }
        }
コード例 #9
0
ファイル: Contract.cs プロジェクト: sharwell/RuntimeContracts
        public static void AssertDebug(
            [DoesNotReturnIf(false)]
            bool condition,
#if NETSTANDARD2_0
            [Localizable(false)]
#endif
            string userMessage                = null,
            [CallerFilePath] string path      = null,
            [CallerLineNumber] int lineNumber = 0)
        {
            if (!condition)
            {
                ContractRuntimeHelper.ReportFailure(ContractFailureKind.Assert, userMessage, null, new Provenance(path, lineNumber));
            }
        }
コード例 #10
0
        public static void Requires <TException>(
            bool condition,
#if NETSTANDARD2_0
            [Localizable(false)]
#endif
            string userMessage                = null,
            [CallerFilePath] string path      = "",
            [CallerLineNumber] int lineNumber = 0) where TException : Exception
#if !NETSTANDARD2_0
        // Previous version are relies on new constraint for exception construction.
        , new()
#endif
        {
            if (!condition)
            {
                ContractRuntimeHelper.ReportPreconditionFailure <TException>(userMessage, null, new Provenance(path, lineNumber));
            }
        }
コード例 #11
0
        /// <summary>
        /// Helper method that throws <see cref="ContractException"/> uncodintionally.
        /// This allows e.g. <code>throw Contract.AssertFailure("Oh no!");</code>
        /// </summary>
        public static Exception AssertFailure(
            // Disable localization prevents CA2204
#if NETSTANDARD2_0
            [Localizable(false)]
#endif
            string message = null,
            [CallerFilePath] string path      = null,
            [CallerLineNumber] int lineNumber = 0)
        {
            Assert(false);
            // This method should fail regardless of the ContractFailEvent handlers.
            ContractRuntimeHelper.RaiseContractFailedEvent(
                ContractFailureKind.Assert,
                message,
                null,
                new Provenance(path, lineNumber),
                out var text);

            return(new ContractException(ContractFailureKind.Assert, text, message, null));
        }