Exemple #1
0
        /// <summary>
        /// Wraps the given exception into a SelfDocumentingException instance after adding the instance variables,
        /// method parameters and local variables to it
        /// </summary>
        /// <param name="e">The exception to wrap.</param>
        /// <param name="message">The message for the SelfDocumentingException</param>
        /// <param name="methodName">The fully qualified method name from where the exception is thrown.</param>
        /// <param name="instanceVarsNames">The names of the instance variables.</param>
        /// <param name="instanceVars">The values of the instance variables at time of exception.</param>
        /// <param name="parameterVarsNames">The names of the method parameters.</param>
        /// <param name="parameterVars">The values of the method parameters at time of exception.</param>
        /// <param name="localVarsNames">The names of the local variables.</param>
        /// <param name="localVars">The values of the local variables at time of exception.</param>
        /// <param name="exceptionToThrow">The type of exception (deriving from SDE) to throw.</param>
        /// <returns>The formed SelfDocumentingException instance.</returns>
        internal static SelfDocumentingException GetSDE(Exception e, string message, string methodName,
                                                        string[] instanceVarsNames, object[] instanceVars,
                                                        string[] parameterVarsNames, object[] parameterVars,
                                                        string[] localVarsNames, object[] localVars,
                                                        Type exceptionToThrow)
        {
            //Wrap only if it is not already of type SelfDocumentingException
            SelfDocumentingException sde = null;

            if (e is SelfDocumentingException)
            {
                sde = (SelfDocumentingException)e;
            }
            else
            {
                //Create instance of the actual exception type to throw
                sde = (SelfDocumentingException)exceptionToThrow.GetConstructor(
                    new Type[] { typeof(string), typeof(Exception) }).Invoke(new object[] { message, e });
            }

            MethodState ms = sde.PinMethod(methodName, e.StackTrace);

            //Add instance variables, method parameters and local variables
            for (int i = 0; i < instanceVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddInstanceVariable(instanceVarsNames[i], instanceVars[i]);
                }
                catch { }
            }
            for (int i = 0; i < parameterVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddMethodParameter(parameterVarsNames[i], parameterVars[i]);
                }
                catch { }
            }
            for (int i = 0; i < localVarsNames.Length; i++)
            {
                //Ignore if unable to add object to MethodState.
                //This is a bug with SDE component. A class with setter only property cannot be added.
                try
                {
                    ms.AddLocalVariable(localVarsNames[i], localVars[i]);
                }
                catch { }
            }

            ms.Lock();
            return(sde);
        }
Exemple #2
0
        /// <summary>
        /// <para>
        /// Constructs a <see cref="SelfDocumentingException"/> instance with all related data.
        /// </para>
        /// </summary>
        ///
        /// <param name="sde">
        /// The <see cref="SelfDocumentingException"/> instance.
        /// </param>
        /// <param name="instanceNames">
        /// the instance variable names.
        /// </param>
        /// <param name="instanceValues">
        /// the instance variable values.
        /// </param>
        /// <param name="paramNames">
        /// The parameter variable names.
        /// </param>
        /// <param name="paramValues">
        /// The parameter variable values.
        /// </param>
        /// <param name="localNames">
        /// The local variable names.
        /// </param>
        /// <param name="localValues">
        /// The local variable values.
        /// </param>
        /// <param name="stackFrameIndex">
        /// The index to the stack frame where the method base will be
        /// obtained from the stack trace.
        /// </param>
        ///
        /// <returns>
        /// The <see cref="SelfDocumentingException"/> instance.
        /// </returns>
        internal static SelfDocumentingException ConstructSDE(
            SelfDocumentingException sde,
            string[] instanceNames, object[] instanceValues,
            string[] paramNames, object[] paramValues,
            string[] localNames, object[] localValues,
            int stackFrameIndex)
        {
            // Pin the method. An index is used to determine which particular stack frame to get the method from.
            MethodBase methodBase = new StackTrace().GetFrame(stackFrameIndex).GetMethod();

            MethodState ms = sde.PinMethod(methodBase.DeclaringType.FullName + "." + methodBase.Name,
                                           ((sde.InnerException == null) ? sde : sde.InnerException).StackTrace);

            // Add instance variables
            if (instanceNames != null)
            {
                for (int i = 0; ((i < instanceNames.Length) && (i < instanceValues.Length)); i++)
                {
                    ms.AddInstanceVariable(instanceNames[i], instanceValues[i]);
                }
            }

            // Add parameter variables
            if (paramNames != null)
            {
                for (int i = 0; ((i < paramNames.Length) && (i < paramValues.Length)); i++)
                {
                    ms.AddMethodParameter(paramNames[i], paramValues[i]);
                }
            }

            // Add local variables
            if (localNames != null)
            {
                for (int i = 0; ((i < localNames.Length) && (i < localValues.Length)); i++)
                {
                    ms.AddLocalVariable(localNames[i], localValues[i]);
                }
            }

            ms.Lock();

            return(sde);
        }