Пример #1
0
        protected override ConstructorInfo GetGreediestSatisfiableConstructor(IPicoContainer container)
        {
            ConstructorInfo greediestConstructor         = null;
            IList           conflicts                    = new ArrayList();
            IList           unsatisfiableDependencyTypes = new ArrayList();

            if (sortedMatchingConstructors == null)
            {
                sortedMatchingConstructors = GetSortedMatchingConstructors();
            }
            int lastSatisfiableConstructorSize = -1;

            for (int i = 0; i < sortedMatchingConstructors.Count; i++)
            {
                bool            failedDependency  = false;
                ConstructorInfo constructor       = (ConstructorInfo)sortedMatchingConstructors[i];
                Type[]          parameterTypes    = TypeUtils.GetParameterTypes(constructor.GetParameters());
                IParameter[]    currentParameters = parameters != null
                                                     ? parameters
                                                     : CreateDefaultParameters(parameterTypes);

                // remember: all constructors with less arguments than the given parameters are filtered out already
                for (int j = 0; j < currentParameters.Length; j++)
                {
                    // check wether this constructor is statisfiable
                    if (currentParameters[j].IsResolvable(container, this, parameterTypes[j]))
                    {
                        continue;
                    }

                    foreach (Type type in parameterTypes)
                    {
                        unsatisfiableDependencyTypes.Add(type);
                    }
                    failedDependency = true;
                    break;
                }

                if (greediestConstructor != null && parameterTypes.Length != lastSatisfiableConstructorSize)
                {
                    if (conflicts.Count == 0)
                    {
                        // we found our match [aka. greedy and satisfied]
                        return(greediestConstructor);
                    }
                    else
                    {
                        // fits although not greedy
                        conflicts.Add(constructor);
                    }
                }
                else if (!failedDependency && lastSatisfiableConstructorSize == parameterTypes.Length)
                {
                    // satisfied and same size as previous one?
                    conflicts.Add(constructor);
                    conflicts.Add(greediestConstructor);
                }
                else if (!failedDependency)
                {
                    greediestConstructor           = constructor;
                    lastSatisfiableConstructorSize = parameterTypes.Length;
                }
            }
            if (conflicts.Count != 0)
            {
                throw new TooManySatisfiableConstructorsException(ComponentImplementation, conflicts);
            }
            else if (greediestConstructor == null && unsatisfiableDependencyTypes.Count != 0)
            {
                throw new UnsatisfiableDependenciesException(this, unsatisfiableDependencyTypes);
            }
            else if (greediestConstructor == null)
            {
                // be nice to the user, show all constructors that were filtered out
                StringBuilder     ctorDetails  = new StringBuilder();
                ConstructorInfo[] constructors = GetAllConstructorInfo();

                foreach (ConstructorInfo constructor in constructors)
                {
                    ctorDetails.Append(TypeUtils.ConstructorAsString(constructor));
                }
                throw new PicoInitializationException("Either do the specified parameters not match any of the following constructors: "
                                                      + ctorDetails
                                                      + " or the constructors were not accessible for '" +
                                                      ComponentImplementation + "'");
            }
            return(greediestConstructor);
        }