Exemplo n.º 1
0
        void IContainerPolicyCreator.CreatePolicies(
            IPolicyList policyList,
            string instanceName,
            ConfigurationElement configurationObject,
            IConfigurationSource configurationSource)
        {
            T castConfigurationObject = (T)configurationObject;
            ConstructorInfo ctor      = castConfigurationObject.Type.GetConstructor(
                BindingFlags.Public | BindingFlags.Instance, null, constructorTypes, null);

            if (ctor == null)
            {
                throw new ArgumentException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.ExceptionCustomProviderTypeDoesNotHaveTheRequiredConstructor,
                              castConfigurationObject.Type.AssemblyQualifiedName),
                          "configurationObject");
            }
            SelectedConstructor selectedCtor = new SelectedConstructor(ctor);
            string parameterKey = Guid.NewGuid().ToString();

            selectedCtor.AddParameterKey(parameterKey);
            policyList.Set <IConstructorSelectorPolicy>(
                new FixedConstructorSelectorPolicy(selectedCtor),
                new NamedTypeBuildKey(castConfigurationObject.Type, instanceName));
            policyList.Set <IDependencyResolverPolicy>(
                new ConstantResolverPolicy(new NameValueCollection(castConfigurationObject.Attributes)),
                parameterKey);
        }
Exemplo n.º 2
0
            public SelectedConstructor SelectConstructor(IBuilderContext context)
            {
                SelectedConstructor selectedConstructor
                    = new SelectedConstructor(typeof(InjectedObject).GetConstructor(new Type[] { typeof(object) }));

                selectedConstructor.AddParameterKey("InjectedObject_injectedValue");
                context.PersistentPolicies.Set <IDependencyResolverPolicy>(this.resolverPolicy, "InjectedObject_injectedValue");

                return(selectedConstructor);
            }
Exemplo n.º 3
0
            public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPoliciesDestination)
            {
                var selectedConstructor = new SelectedConstructor(typeof(T).GetConstructor(new[] { typeof(object) }));
                var key = Guid.NewGuid().ToString();

                selectedConstructor.AddParameterKey(key);
                resolverPoliciesDestination.Set <IDependencyResolverPolicy>(this.parameterResolverPolicy, key);

                return(selectedConstructor);
            }
Exemplo n.º 4
0
        private void InitializeForCreationWithConstructor(NewExpression newExpression, ParameterExpression parameter)
        {
            ConstructorInfo constructor = newExpression.Constructor;
            // TODO check accessibility?

            // keep a local reference to the selected constructor until it's done building
            SelectedConstructor tempSelectedConstructor = new SelectedConstructor(constructor);

            SetUpCallParameters(parameter,
                                newExpression.Arguments,
                                constructor, s => tempSelectedConstructor.AddParameterKey(s));

            // by now the selected constructor is sane
            this.selectedConstructor = tempSelectedConstructor;
        }
			private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type interceptingType)
			{
				var originalParams = originalConstructor.Constructor.GetParameters();

				var newConstructorInfo = interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray());

				var newConstructor = new SelectedConstructor(newConstructorInfo);

				foreach (var key in originalConstructor.GetParameterKeys())
				{
					newConstructor.AddParameterKey(key);
				}

				return newConstructor;
			}
Exemplo n.º 6
0
            private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type interceptingType)
            {
                ParameterInfo[] originalParams = originalConstructor.Constructor.GetParameters();

                ConstructorInfo newConstructorInfo =
                    interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray());

                SelectedConstructor newConstructor = new SelectedConstructor(newConstructorInfo);

                foreach (string key in originalConstructor.GetParameterKeys())
                {
                    newConstructor.AddParameterKey(key);
                }

                return(newConstructor);
            }
Exemplo n.º 7
0
        private static SelectedConstructor FindNewConstructor(SelectedConstructor originalConstructor, Type proxyType)
        {
            ParameterInfo[] originalParams = originalConstructor.Constructor.GetParameters();

            ConstructorInfo newConstructorInfo = proxyType.GetConstructor(Seq.Make(originalParams)
                                                                          .Map <Type>(delegate(ParameterInfo pi) { return(pi.ParameterType); }).ToArray());

            SelectedConstructor newConstructor = new SelectedConstructor(newConstructorInfo);

            foreach (string key in originalConstructor.GetParameterKeys())
            {
                newConstructor.AddParameterKey(key);
            }

            return(newConstructor);
        }
Exemplo n.º 8
0
        private bool TryToMatchAndAddPolicies(
            ConstructorInfo ctor,
            ConfigurationElement configurationObject,
            IPolicyList policyList,
            string instanceName)
        {
            Type configurationObjectType = configurationObject.GetType();

            ParameterInfo[]     constructorParameters = ctor.GetParameters();
            List <PropertyInfo> matchingProperties    = new List <PropertyInfo>();

            foreach (ParameterInfo parameter in constructorParameters)
            {
                PropertyInfo matchingProperty
                    = configurationObjectType.GetProperty(
                          parameter.Name,
                          BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (!(matchingProperty != null &&
                      matchingProperty.CanRead &&
                      matchingProperty.GetIndexParameters().Length == 0))
                {
                    return(false);
                }
                if (!MatchArgumentAndPropertyTypes(parameter, matchingProperty))
                {
                    return(false);
                }
                matchingProperties.Add(matchingProperty);
            }
            SelectedConstructor selectedConstructor = new SelectedConstructor(ctor);

            for (int i = 0; i < constructorParameters.Length; i++)
            {
                ParameterInfo parameterInfo = constructorParameters[i];
                PropertyInfo  propertyInfo  = matchingProperties[i];
                string        parameterKey  = Guid.NewGuid().ToString();
                object        value         = propertyInfo.GetValue(configurationObject, null);
                policyList.Set <IDependencyResolverPolicy>(CreateDependencyResolverPolicy(parameterInfo, value), parameterKey);
                selectedConstructor.AddParameterKey(parameterKey);
            }
            policyList.Set <IConstructorSelectorPolicy>(
                new FixedConstructorSelectorPolicy(selectedConstructor),
                new NamedTypeBuildKey(targetType, instanceName));
            return(true);
        }
Exemplo n.º 9
0
            public SelectedConstructor SelectConstructor(IBuilderContext context, IPolicyList resolverPoliciesDestination)
            {
                SelectedConstructor selectedConstructor
                    = new SelectedConstructor(typeof(InjectedObject).GetMatchingConstructor(new Type[] { typeof(object) }));
                selectedConstructor.AddParameterKey("InjectedObject_injectedValue");
                resolverPoliciesDestination.Set<IDependencyResolverPolicy>(this.resolverPolicy, "InjectedObject_injectedValue");

                return selectedConstructor;
            }