示例#1
0
        /// <summary>
        /// Returns sequence of properties on the given type that
        /// should be set as part of building that object.
        /// </summary>
        /// <param name="context">Current build context.</param>
        /// <param name="resolverPolicyDestination">The <see cref='IPolicyList'/> to add any
        /// generated resolver objects into.</param>
        /// <returns>Sequence of <see cref="PropertyInfo"/> objects
        /// that contain the properties to set.</returns>
        public IEnumerable <SelectedProperty> SelectProperties(IBuilderContext context, IPolicyList resolverPolicyDestination)
        {
            Type typeToInjection = context.BuildKey.Type;

            foreach (var property in typeToInjection.GetPropertiesHierarchical().Where(p => p.CanWrite))
            {
                var propertyMethod = property.SetMethod ?? property.GetMethod;
                if (propertyMethod.IsStatic)
                {
                    // Skip static properties. In the previous implementation the reflection query took care of this.
                    continue;
                }

                // Ignore indexers and return properties marked with the attribute
                if (property.GetIndexParameters().Length == 0 &&
                    (property.IsDefined(typeof(InjectionAttribute), false) || property.IsDefined(typeof(DependencyResolutionAttribute), false)))
                {
                    IDependencyResolverPolicy resolver;
                    var dependencyAttribute = property.GetCustomAttribute <DependencyResolutionAttribute>();
                    if (dependencyAttribute != null)
                    {
                        resolver = dependencyAttribute.CreateResolver(property.PropertyType);
                    }
                    else
                    {
                        resolver = new NamedTypeDependencyResolverPolicy(property.PropertyType, null);
                    }
                    yield return(new SelectedProperty(property, resolver));
                }
            }
        }
        public override void PreBuildUp(IBuilderContext context)
        {
            Guard.AssertNotNull(context, "context");
            Guard.AssertNotNull(context.BuildKey, "context.BuildKey");
            Guard.AssertNotNull(context.Policies, "context.Policies");

            if (context.Policies.Get <IMapParameterNameToRegistrationNamePolicy>(context.BuildKey) == null)
            {
                return;
            }

            IPolicyList resolverPolicyDestination;
            IConstructorSelectorPolicy selector = context.Policies.Get <IConstructorSelectorPolicy>(context.BuildKey, out resolverPolicyDestination);

            if (selector == null)
            {
                return;
            }

            var selectedConstructor = selector.SelectConstructor(context, resolverPolicyDestination);

            if (selectedConstructor == null)
            {
                return;
            }

            ParameterInfo[] parameters = selectedConstructor.Constructor.GetParameters();

            //string[] parameterKeys = selectedConstructor.GetParameterKeys();

            for (int i = 0; i < parameters.Length; i++)
            {
                Type parameterType = parameters[i].ParameterType;

                if (parameterType.IsAbstract || parameterType.IsInterface || (parameterType.IsClass && parameterType != typeof(string)))
                {
                    IDependencyResolverPolicy resolverPolicy = new NamedTypeDependencyResolverPolicy(parameterType, parameters[i].Name);
                    //context.Policies.Set<IDependencyResolverPolicy>(resolverPolicy, parameterKeys[i]);
                    context.Policies.Set <IDependencyResolverPolicy>(resolverPolicy, parameters[i].Name);
                }
            }

            resolverPolicyDestination.Set <IConstructorSelectorPolicy>(new SelectedConstructorCache(selectedConstructor), context.BuildKey);
        }