Exemplo n.º 1
0
        public static string RaiseContractFailedEvent(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException)
        {
            string resultFailureMessage = "Contract failed";

            ContractHelper.RaiseContractFailedEventImplementation(failureKind, userMessage, conditionText, innerException, ref resultFailureMessage);
            return(resultFailureMessage);
        }
Exemplo n.º 2
0
        private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", (object)failureKind), "failureKind");
            }
            string str1 = "contract failed.";
            ContractFailedEventArgs e = (ContractFailedEventArgs)null;

            RuntimeHelpers.PrepareConstrainedRegions();
            string str2;

            try
            {
                str1 = ContractHelper.GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> eventHandler = ContractHelper.contractFailedEvent;
                if (eventHandler != null)
                {
                    e = new ContractFailedEventArgs(failureKind, str1, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> invocation in eventHandler.GetInvocationList())
                    {
                        try
                        {
                            invocation((object)null, e);
                        }
                        catch (Exception ex)
                        {
                            e.thrownDuringHandler = ex;
                            e.SetUnwind();
                        }
                    }
                    if (e.Unwind)
                    {
                        if (Environment.IsCLRHosted)
                        {
                            ContractHelper.TriggerCodeContractEscalationPolicy(failureKind, str1, conditionText, innerException);
                        }
                        if (innerException == null)
                        {
                            innerException = e.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, str1, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                str2 = e == null || !e.Handled ? str1 : (string)null;
            }
            resultFailureMessage = str2;
        }
Exemplo n.º 3
0
        private static void TriggerFailureImplementation(ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
        {
            if (Environment.IsCLRHosted)
            {
                ContractHelper.TriggerCodeContractEscalationPolicy(kind, displayMessage, conditionText, innerException);
                throw new ContractException(kind, displayMessage, userMessage, conditionText, innerException);
            }
            if (!Environment.UserInteractive)
            {
                throw new ContractException(kind, displayMessage, userMessage, conditionText, innerException);
            }
            string resourceString = Environment.GetResourceString(ContractHelper.GetResourceNameForFailure(kind));

            Assert.Fail(conditionText, displayMessage, resourceString, -2146233022, StackTrace.TraceFormat.Normal, 2);
        }
Exemplo n.º 4
0
        private static string GetDisplayMessage(ContractFailureKind failureKind, string userMessage, string conditionText)
        {
            string resourceNameForFailure = ContractHelper.GetResourceNameForFailure(failureKind);
            string resourceString;

            if (!string.IsNullOrEmpty(conditionText))
            {
                resourceString = Environment.GetResourceString(resourceNameForFailure + "_Cnd", (object)conditionText);
            }
            else
            {
                resourceString = Environment.GetResourceString(resourceNameForFailure);
            }
            if (!string.IsNullOrEmpty(userMessage))
            {
                return(resourceString + "  " + userMessage);
            }
            return(resourceString);
        }
Exemplo n.º 5
0
 public static void TriggerFailure(ContractFailureKind kind, string displayMessage, string userMessage, string conditionText, Exception innerException)
 {
     ContractHelper.TriggerFailureImplementation(kind, displayMessage, userMessage, conditionText, innerException);
 }
        private static void RaiseContractFailedEventImplementation(ContractFailureKind failureKind, string userMessage, string conditionText, Exception innerException, ref string resultFailureMessage)
        {
            if (failureKind < ContractFailureKind.Precondition || failureKind > ContractFailureKind.Assume)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_EnumIllegalVal", new object[]
                {
                    failureKind
                }), "failureKind");
            }
            string text = "contract failed.";
            ContractFailedEventArgs contractFailedEventArgs = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            string text2;

            try
            {
                text = ContractHelper.GetDisplayMessage(failureKind, userMessage, conditionText);
                EventHandler <ContractFailedEventArgs> eventHandler = ContractHelper.contractFailedEvent;
                if (eventHandler != null)
                {
                    contractFailedEventArgs = new ContractFailedEventArgs(failureKind, text, conditionText, innerException);
                    foreach (EventHandler <ContractFailedEventArgs> eventHandler2 in eventHandler.GetInvocationList())
                    {
                        try
                        {
                            eventHandler2(null, contractFailedEventArgs);
                        }
                        catch (Exception thrownDuringHandler)
                        {
                            contractFailedEventArgs.thrownDuringHandler = thrownDuringHandler;
                            contractFailedEventArgs.SetUnwind();
                        }
                    }
                    if (contractFailedEventArgs.Unwind)
                    {
                        if (Environment.IsCLRHosted)
                        {
                            ContractHelper.TriggerCodeContractEscalationPolicy(failureKind, text, conditionText, innerException);
                        }
                        if (innerException == null)
                        {
                            innerException = contractFailedEventArgs.thrownDuringHandler;
                        }
                        throw new ContractException(failureKind, text, userMessage, conditionText, innerException);
                    }
                }
            }
            finally
            {
                if (contractFailedEventArgs != null && contractFailedEventArgs.Handled)
                {
                    text2 = null;
                }
                else
                {
                    text2 = text;
                }
            }
            resultFailureMessage = text2;
        }