Catel.IoC.TypeFactory.TryCreateToConstruct C# (CSharp) Метод

TryCreateToConstruct() приватный Метод

Tries to create the service with the specified constructor using the specified parameters. This method will not throw an exception when the invocation fails.
Note that this method does not require an implementation of TypeRequestPath because this already has the parameter values and thus cannot lead to invalid circular dependencies.
private TryCreateToConstruct ( Type typeToConstruct, ConstructorInfo constructor, object tag, object parameters, bool checkConstructor, bool hasMoreConstructorsLeft ) : object
typeToConstruct System.Type Type of the service.
constructor System.Reflection.ConstructorInfo The constructor info.
tag object The preferred tag when resolving dependencies.
parameters object The parameters to pass into the constructor.
checkConstructor bool if set to true, check whether the constructor can be used before using it.
hasMoreConstructorsLeft bool if set to true, more constructors are left so don't throw exceptions.
Результат object
        private object TryCreateToConstruct(Type typeToConstruct, ConstructorInfo constructor, object tag, object[] parameters,
            bool checkConstructor, bool hasMoreConstructorsLeft)
        {
            // Check if this constructor is even possible
            if (checkConstructor)
            {
                if (!CanConstructorBeUsed(constructor, tag, true, parameters))
                {
                    return null;
                }
            }

            try
            {
                var finalParameters = new List<object>(parameters);
                var ctorParameters = constructor.GetParameters();
                for (int i = parameters.Length; i < ctorParameters.Length; i++)
                {
                    object ctorParameterValue = null;

                    if (tag != null && _serviceLocator.IsTypeRegistered(ctorParameters[i].ParameterType, tag))
                    {
                        // Use preferred tag
                        ctorParameterValue = _serviceLocator.ResolveType(ctorParameters[i].ParameterType, tag);
                    }
                    else
                    {
                        // No tag or fallback to default without tag
                        ctorParameterValue = _serviceLocator.ResolveType(ctorParameters[i].ParameterType);
                    }

                    finalParameters.Add(ctorParameterValue);
                }

                var finalParametersArray = finalParameters.ToArray();

                Log.Debug("Calling constructor.Invoke with the right parameters");

                var instance = constructor.Invoke(finalParametersArray);

                InitializeAfterConstruction(instance);

                return instance;
            }
#if NET
            catch (MissingMethodException)
            {
                // Ignore, we accept this
            }
#endif
            catch (TargetParameterCountException)
            {
                // Ignore, we accept this
            }
            catch (CircularDependencyException)
            {
                // Only handle CircularDependencyExceptions we throw ourselves because we support generic types such as 
                // Dictionary<TKey, TValue> which has a constructor with IDictionary<TKey, TValue>
                if (!hasMoreConstructorsLeft)
                //if (string.Equals(TypeRequestPathName, ex.TypePath.Name, StringComparison.Ordinal))
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                // Real exceptions bubble up, otherwise return null
                Log.Error(ex, "Failed to instantiate type '{0}', but this was an unexpected error", typeToConstruct.FullName);
                throw;
            }

            Log.Debug("Failed to create instance using dependency injection for type '{0}' using constructor '{1}'",
                      typeToConstruct.FullName, constructor.GetSignature());

            return null;
        }