示例#1
0
        public object Produce(Container container, Registration registration)
        {
            // try to product via the specific producer
            if (registration.ImplementationProducer != null)
            {
                var cache = GetCachedLifestyle(registration.Lifestyle);
                return cache.GetInstance(registration.ImplementationProducer);
            }

            // otherwise always try and resolve as many parameters as possible
            Type resolveType = registration.ImplementationType;
            var constructors = resolveType.GetConstructors().OrderByDescending(c => c.GetParameters().Count());
            foreach (var constructor in constructors)
            {
                try
                {
                    return Produce(container, registration, constructor);
                }
                catch 
                {
                }
            }

            // if we make it here for some reason it's an error
            throw new Exception(string.Format("Could not resolve type {0}", registration.ServiceType.FullName));
        }
示例#2
0
        private object Produce(Container container, Registration registration, ConstructorInfo constructor)
        {
            var dependencies = constructor.GetParameters();
            var cache = GetCachedLifestyle(registration.Lifestyle);

            if (dependencies.Count() == 0)
            {
                return cache.GetInstance(constructor);
            }

            IList<object> parameters = new List<object>();
            foreach (var dependency in dependencies)
            {
                var paramRegistration = container.GetRegistration(dependency.ParameterType);
                parameters.Add(Produce(container, paramRegistration));
            }

            return cache.GetInstance(constructor, parameters.ToArray());
        }