示例#1
0
        protected void inject(MethodInfo methodInfo, object instance, IDependencyResolver dependencyResolver, IDependencyResolver explicitDependencies)
        {
            ParameterInfo[] parameterInfos  = methodInfo.GetParameters();
            object[]        parameterValues = new object[parameterInfos.Length];

            InjectionAttribute atr   = (InjectionAttribute)methodInfo.GetCustomAttributes(typeof(InjectionAttribute), true)[0];
            ResolutionOrder    order = atr.resolution;
            bool soft = atr.soft;

            for (int i = 0; i < parameterInfos.Length; i++)
            {
                ParameterInfo parameter = parameterInfos[i];

                object value = ResolveDependency(order, soft, instance,
                                                 parameter.ParameterType, parameter.Name, dependencyResolver, explicitDependencies);

                if (value != null)
                {
                    ControlRemoteObject(instance, value, methodInfo, parameter);
                }

                parameterValues[i] = value;
            }

            methodInfo.Invoke(instance, parameterValues);
        }
        protected object ResolveDependency(ResolutionOrder order, bool soft,
                                           object instance, Type type, string name,
                                           IDependencyResolver resolver, IDependencyResolver explicitResolver)
        {
            object value = null;

            if (explicitResolver != null)
            {
                value = TryResolveExplicitDependency(type, name, explicitResolver);
            }

            if (value == null && order == ResolutionOrder.FirstExplicitThanContext)
            {
                value = resolver.TryResolve(type, null, false);
            }

            if (value != null)
            {
                return(value);
            }
            if (!soft)
            {
                ThrowResolutionException(type, name, instance, null);
            }

            return(value);
        }
        public override void inject(object instance, IDependencyResolver dependencyResolver, IDependencyResolver explicitDependencies)
        {
            foreach (PropertyInfo propertyInfo in _properties)
            {
                InjectionAttribute atr   = (InjectionAttribute)propertyInfo.GetCustomAttributes(typeof(InjectionAttribute), true)[0];
                ResolutionOrder    order = atr.resolution;
                bool soft = atr.soft;

                object value = ResolveDependency(order, soft, instance,
                                                 propertyInfo.PropertyType, propertyInfo.Name, dependencyResolver, explicitDependencies);

                if (value != null)
                {
                    ControlRemoteObject(instance, value, propertyInfo);
                    propertyInfo.SetValue(instance, value, null);
                }
            }
        }