예제 #1
0
        private static bool CanResolveParameter(ICherryServiceLocatorAndRegistry current,
            ParameterInfo constructorParameterInfo, InjectionParameter[] parameters)
        {
            if (parameters == null || parameters.Length == 0)
            {
                // Shotcut when no parameters provided
                return current.CanGet(constructorParameterInfo.ParameterType);
            }

            InjectionParameter namedParameter = parameters.SingleOrDefault(p => p.Key == constructorParameterInfo.Name);
            if (namedParameter != null)
            {
                return true;
            }

            InjectionParameter unnamedParameter = parameters.SingleOrDefault(
                p => string.IsNullOrEmpty(p.Key) &&
                     !ReferenceEquals(p.Value, null)
                     && constructorParameterInfo.ParameterType.IsInstanceOfType(p.Value));
            if (unnamedParameter != null)
            {
                return true;
            }

            return current.CanGet(constructorParameterInfo.ParameterType);
        }
예제 #2
0
        public object Get(ICherryServiceLocatorAndRegistry original, ICherryServiceLocatorAndRegistry current,
            InjectionParameter[] parameters)
        {
            ConstructorInfo[] allPublicConstructors = _targetType.GetConstructors();

            ConstructorInfo bestConstructor =
                allPublicConstructors.OrderByDescending(
                    c => c.GetParameters().Count(p => CanResolveParameter(current, p, parameters)))
                    .FirstOrDefault();

            if (bestConstructor == null)
            {
                throw new InvalidOperationException(
                    string.Format(
                        "Creating an instance of type \"{0}\" failed. This type has no public constructors that can be satisfied.",
                        _targetType));
            }

            ParameterInfo[] constructorParameterInfos = bestConstructor.GetParameters();

            ValidateParameters(constructorParameterInfos, parameters);

            var constructorParameters = new object[constructorParameterInfos.Length];
            for (int i = 0; i < constructorParameterInfos.Length; i++)
            {
                constructorParameters[i] = ResolveParameter(original, current, constructorParameterInfos, i, parameters);
            }

            object instance = bestConstructor.Invoke(constructorParameters);
            return instance;
        }
 public object Get(ICherryServiceLocatorAndRegistry original, ICherryServiceLocatorAndRegistry current, InjectionParameter[] parameters)
 {
     if (parameters != null && parameters.Length > 0)
     {
         throw new ArgumentException(string.Format("Since \"{0}\" is a singleton instance, you cannot use parameters to resolve it.", _instance), "parameters");
     }
     return _instance;
 }
예제 #4
0
        public object Get(ICherryServiceLocatorAndRegistry original, ICherryServiceLocatorAndRegistry current, InjectionParameter[] parameters)
        {
            if (parameters != null && parameters.Length > 0)
            {
                throw new ArgumentException(string.Format("Since \"{0}\" is a singleton instance, you cannot use parameters to resolve it.", _instance), "parameters");
            }

            if (_instance != null)
            {
                return _instance;
            }
            lock (_syncRoot)
            {
                if (_instance != null)
                {
                    return _instance;
                }
                var instance = (new PerResolveResolver(_targetType)).Get(original, current, parameters);
                _instance = instance;
                return instance;
            }
        }
        private object GetInternal(ICherryServiceLocatorAndRegistry originalLocator, Type serviceKey,
            params InjectionParameter[] parameters)
        {
            if (ReferenceEquals(serviceKey, null))
            {
                throw new ArgumentNullException("serviceKey", "The serviceKey must not be null");
            }

            object factoryMethod;
            if (ServiceLocatorFactoryMethodSupportExtensions.GetFactoryMethod(this, serviceKey, out factoryMethod))
            {
                return factoryMethod;
            }

            if (serviceKey.IsClass && !serviceKey.IsAbstract && !IsRegisteredRecursively(serviceKey))
            {
                var perResolveResolver = new PerResolveResolver(serviceKey);
                var instance = perResolveResolver.Get(originalLocator, this, parameters);
                return instance;
            }

            IResolver resolver;
            if (!_registrations.TryGetValue(serviceKey, out resolver))
            {
                if (_parent != null)
                {
                    return _parent.GetInternal(originalLocator, serviceKey);
                }
                throw new ArgumentException(string.Format("The type {0} could not be resolved.", serviceKey), "serviceKey");
            }
            return resolver.Get(originalLocator, this, parameters);
        }
 object ICherryServiceLocatorAndRegistry.Get(ICherryServiceLocatorAndRegistry originalLocator, Type serviceKey, params InjectionParameter[] parameters)
 {
     return GetInternal(originalLocator, serviceKey, parameters);
 }
예제 #7
0
        private static object ResolveParameter(ICherryServiceLocatorAndRegistry original,
            ICherryServiceLocatorAndRegistry current, ParameterInfo[] constructorParameterInfos, int i,
            InjectionParameter[] parameters)
        {
            ParameterInfo constructorParameterInfo = constructorParameterInfos[i];

            if (parameters == null || parameters.Length == 0)
            {
                // Shotcut when no parameters provided
                return current.Get(original, constructorParameterInfo.ParameterType);
            }

            InjectionParameter namedParameter = parameters.SingleOrDefault(p => p.Key == constructorParameterInfo.Name);
            if (namedParameter != null)
            {
                return namedParameter.Value;
            }

            // No matching named parameter found, so lets try if we have a single parameter that mathes the constructor parameter type
            InjectionParameter unnamedParameter = parameters.SingleOrDefault(
                p => string.IsNullOrEmpty(p.Key) &&
                     !ReferenceEquals(p.Value, null)
                     && constructorParameterInfo.ParameterType.IsInstanceOfType(p.Value));

            if (unnamedParameter != null)
            {
                return unnamedParameter.Value;
            }

            return current.Get(original, constructorParameterInfo.ParameterType);
        }