Пример #1
0
        /// <summary>
        /// Allocates a new aggregate exception with the specified message and list of inner exceptions.
        /// </summary>
        /// <param name="message">The error message that explains the reason for the exception.</param>
        /// <param name="innerExceptions">The exceptions that are the cause of the current exception.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="innerExceptions"/> argument
        /// is null.</exception>
        /// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
        /// null.</exception>
        private AggregateException(string message, IList <Exception> innerExceptions)
            : base(message, innerExceptions != null && innerExceptions.Count > 0 ? innerExceptions[0] : null)
        {
            if (innerExceptions == null)
            {
                throw new ArgumentNullException("innerExceptions");
            }

            // Copy exceptions to our internal array and validate them. We must copy them,
            // because we're going to put them into a ReadOnlyCollection which simply reuses
            // the list passed in to it. We don't want callers subsequently mutating.
            Exception[] exceptionsCopy = new Exception[innerExceptions.Count];

            for (int i = 0; i < exceptionsCopy.Length; i++)
            {
                exceptionsCopy[i] = innerExceptions[i];

                if (exceptionsCopy[i] == null)
                {
                    throw new ArgumentException(Environment2.GetResourceString("AggregateException_ctor_InnerExceptionNull"));
                }
            }

            m_innerExceptions = new ReadOnlyCollection <Exception>(exceptionsCopy);
        }
Пример #2
0
        /// <summary>Creates an instance of T using m_valueFactory in case its not null or use reflection to create a new T()</summary>
        /// <returns>An instance of Boxed.</returns>
        private Boxed CreateValue()
        {
            Boxed boxed = null;
            LazyThreadSafetyMode mode = Mode;

            if (m_valueFactory != null)
            {
                try
                {
                    // check for recursion
                    if (mode != LazyThreadSafetyMode.PublicationOnly && m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED)
                    {
                        throw new InvalidOperationException(Environment2.GetResourceString("Lazy_Value_RecursiveCallsToValue"));
                    }

                    Func <T> factory = m_valueFactory;
                    if (mode != LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes
                    {
                        m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED;
                    }
                    boxed = new Boxed(factory());
                }
                catch (Exception ex)
                {
                    if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode
                    {
#if PFX_LEGACY_3_5
                        m_boxed = new LazyInternalExceptionHolder(ex);
#else
                        m_boxed = new LazyInternalExceptionHolder(ex.PrepForRemoting());// copy the call stack by calling the internal method PrepForRemoting
#endif
                    }
                    throw;
                }
            }
            else
            {
                try
                {
                    boxed = new Boxed((T)Activator.CreateInstance(typeof(T)));
                }
                catch (System.MissingMethodException)
                {
                    Exception ex = new System.MissingMemberException(Environment2.GetResourceString("Lazy_CreateValue_NoParameterlessCtorForT"));
                    if (mode != LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode
                    {
                        m_boxed = new LazyInternalExceptionHolder(ex);
                    }
                    throw ex;
                }
            }

            return(boxed);
        }
Пример #3
0
        /// <summary>
        /// Creates and returns a string representation of the current <see cref="AggregateException"/>.
        /// </summary>
        /// <returns>A string representation of the current exception.</returns>
        public override string ToString()
        {
            string text = base.ToString();

            for (int i = 0; i < m_innerExceptions.Count; i++)
            {
                text = String.Format(
                    CultureInfo.InvariantCulture,
                    Environment2.GetResourceString("AggregateException_ToString"),
                    text, Environment.NewLine, i, m_innerExceptions[i].ToString(), "<---", Environment.NewLine);
            }

            return(text);
        }
Пример #4
0
        protected AggregateException(SerializationInfo info, StreamingContext context) :
            base(info, context)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            Exception[] innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
            if (innerExceptions == null)
            {
                throw new SerializationException(Environment2.GetResourceString("AggregateException_DeserializationFailure"));
            }

            m_innerExceptions = new ReadOnlyCollection <Exception>(innerExceptions);
        }
Пример #5
0
        /// <summary>
        /// Static helper function that returns an object based on the given mode. it also throws an exception if the mode is invalid
        /// </summary>
        private static object GetObjectFromMode(LazyThreadSafetyMode mode)
        {
            if (mode == LazyThreadSafetyMode.ExecutionAndPublication)
            {
                return(new object());
            }
            else if (mode == LazyThreadSafetyMode.PublicationOnly)
            {
                return(PUBLICATION_ONLY_OR_ALREADY_INITIALIZED);
            }
            else if (mode != LazyThreadSafetyMode.None)
            {
                throw new ArgumentOutOfRangeException("mode", Environment2.GetResourceString("Lazy_ctor_ModeInvalid"));
            }

            return(null); // None mode
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateException"/> class with
 /// references to the inner exceptions that are the cause of this exception.
 /// </summary>
 /// <param name="innerExceptions">The exceptions that are the cause of the current exception.</param>
 /// <exception cref="T:System.ArgumentNullException">The <paramref name="innerExceptions"/> argument
 /// is null.</exception>
 /// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
 /// null.</exception>
 public AggregateException(params Exception[] innerExceptions) :
     this(Environment2.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions)
 {
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AggregateException"/> class with
 /// references to the inner exceptions that are the cause of this exception.
 /// </summary>
 /// <param name="innerExceptions">The exceptions that are the cause of the current exception.</param>
 /// <exception cref="T:System.ArgumentNullException">The <paramref name="innerExceptions"/> argument
 /// is null.</exception>
 /// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
 /// null.</exception>
 public AggregateException(IEnumerable <Exception> innerExceptions) :
     this(Environment2.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions)
 {
 }
Пример #8
0
        private ReadOnlyCollection <Exception> m_innerExceptions; // Complete set of exceptions.

        /// <summary>
        /// Initializes a new instance of the <see cref="AggregateException"/> class.
        /// </summary>
        public AggregateException()
            : base(Environment2.GetResourceString("AggregateException_ctor_DefaultMessage"))
        {
            m_innerExceptions = new ReadOnlyCollection <Exception>(new Exception[0]);
        }
Пример #9
0
 public OperationCanceledException2()
     : base(Environment2.GetResourceString("OperationCanceled"))
 {
 }
Пример #10
0
 /// <summary>Creates and returns a string representation of this instance.</summary>
 /// <returns>The result of calling <see cref="System.Object.ToString"/> on the <see
 /// cref="Value"/>.</returns>
 /// <exception cref="T:System.NullReferenceException">
 /// The <see cref="Value"/> is null.
 /// </exception>
 public override string ToString()
 {
     return(IsValueCreated ? Value.ToString() : Environment2.GetResourceString("Lazy_ToString_ValueNotCreated"));
 }