コード例 #1
0
            private object ResolveParameter(Type type, ParameterInfo parameterInfo)
            {
                //Use default parameter value.
                if (parameterInfo.IsOptional && !_container.CanResolve(type))
                {
                    return(parameterInfo.DefaultValue);
                }

                var hasParamsAttr = parameterInfo.IsDefined(typeof(ParamArrayAttribute), false);

                //If have ParamArrayAttribute.
                if (hasParamsAttr)
                {
#if NET4
                    var originalType = type.GetElementType();
#else
                    var originalType = type.GetTypeInfo().GetElementType();
#endif

                    //If exist array binding.
                    if (_container.CanResolve(type))
                    {
                        return(_container.Get(type));
                    }

                    //If exist binding for type.
                    if (_container.CanResolve(originalType))
                    {
                        return(ConvertToArray(type, _container.GetAll(originalType)));
                    }
                    return(ConvertToArray(type, new object[0]));
                }
                return(_container.Get(type));
            }
コード例 #2
0
        public object Get(Type service, string name = null, params IIocParameter[] parameters)
        {
            this.NotBeDisposed();
            Should.NotBeNull(service, nameof(service));
            var key = new BindingKey(service, name);
            BindingRegistration registration = null;

            lock (_bindingRegistrations)
            {
                List <BindingRegistration> list;
                if (_bindingRegistrations.TryGetValue(key, out list))
                {
                    if (list.Count > 1)
                    {
                        throw new InvalidOperationException($"Cannot activate type {service} found more that one binding");
                    }
                    if (list.Count == 1)
                    {
                        registration = list[0];
                    }
                }
            }
            if (registration != null)
            {
                return(registration.Resolve(parameters));
            }
            if (_parent != null && _parent.HasRegistration(key))
            {
                return(_parent.Get(service, name, parameters));
            }
            return(Resolve(service, parameters));
        }