Ejemplo n.º 1
0
        private static void ThrowTypeInitializationException(TypeInitializationException ex)
        {
            // The fact that we're using static constructors to initialize this is an internal detail.
            // Rethrow the inner exception if there is one.
            // Do it carefully so as to not stomp on the original callstack.
#if NET40
            throw ExceptionEnlightenment.PrepareForRethrow(ex.InnerException ?? ex);
#else
            ExceptionDispatchInfo.Capture(ex.InnerException ?? ex).Throw();
#endif
            throw new InvalidOperationException("Unreachable"); // keep the compiler happy
        }
            public object CreateInstance(IServiceProvider provider)
            {
                for (var index = 0; index != _parameters.Length; index++)
                {
                    if (_parameterValuesSet[index] == false)
                    {
                        var value = provider.GetService(_parameters[index].ParameterType);
                        if (value == null)
                        {
                            if (!_parameters[index].HasDefaultValueEx()) // ## ¿àÖñ ÐÞ¸Ä ##
                            {
                                throw new InvalidOperationException($"Unable to resolve service for type '{_parameters[index].ParameterType}' while attempting to activate '{_constructor.DeclaringType}'.");
                            }
                            else
                            {
                                _parameterValues[index] = _parameters[index].DefaultValue;
                            }
                        }
                        else
                        {
                            _parameterValues[index] = value;
                        }
                    }
                }

                try
                {
                    return(_constructor.Invoke(_parameterValues));
                }
                catch (Exception ex)
                {
                    // ## ¿àÖñ ÐÞ¸Ä ##
#if NET40
                    throw ExceptionEnlightenment.PrepareForRethrow(ex.InnerException);
#else
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
#endif
                    // The above line will always throw, but the compiler requires we throw explicitly.
                    throw;
                }
            }
Ejemplo n.º 3
0
 /// <summary>
 /// Attempts to prepare the exception for re-throwing by preserving the stack trace. The returned exception should be immediately thrown.
 /// </summary>
 /// <param name="exception">The exception. May not be <c>null</c>.</param>
 /// <returns>The <see cref="Exception"/> that was passed into this method.</returns>
 public static Exception PrepareForRethrow(Exception exception)
 {
     return(ExceptionEnlightenment.PrepareForRethrow(exception));
 }