コード例 #1
0
        /// <summary>
        /// "autowire constructor" (with constructor arguments by type) behavior.
        /// Also applied if explicit constructor argument values are specified,
        /// matching all remaining arguments with objects from the object factory.
        /// </summary>
        /// <para>
        /// This corresponds to constructor injection: In this mode, a Spring
        /// object factory is able to host components that expect constructor-based
        /// dependency resolution.
        /// </para>
        /// <param name="objectName">Name of the object.</param>
        /// <param name="rod">The merged object definition for the object.</param>
        /// <param name="chosenCtors">The chosen chosen candidate constructors (or <code>null</code> if none).</param>
        /// <param name="explicitArgs">The explicit argument values passed in programmatically via the getBean method,
        /// or <code>null</code> if none (-> use constructor argument values from object definition)</param>
        /// <returns>An IObjectWrapper for the new instance</returns>
        public IObjectWrapper AutowireConstructor(string objectName, RootObjectDefinition rod,
                                                  ConstructorInfo[] chosenCtors, object[] explicitArgs)
        {
            ObjectWrapper wrapper = new ObjectWrapper();

            ConstructorInstantiationInfo constructorInstantiationInfo = GetConstructorInstantiationInfo(
                objectName, rod, chosenCtors, explicitArgs);

            wrapper.WrappedInstance = instantiationStrategy.Instantiate(rod, objectName, this.objectFactory,
                                                                        constructorInstantiationInfo.ConstructorInfo, constructorInstantiationInfo.ArgInstances);

            if (log.IsDebugEnabled)
            {
                log.Debug(string.Format(CultureInfo.InvariantCulture, "Object '{0}' instantiated via constructor [{1}].", objectName, constructorInstantiationInfo.ConstructorInfo));
            }

            return(wrapper);
        }
コード例 #2
0
        public void CanApplyConstructorArgsToAbstractType()
        {
            IResource        resource = new ReadOnlyXmlTestResource("ctor-args.xml", GetType());
            XmlObjectFactory xof      = new XmlObjectFactory(resource);
            TestObject       rod      = (TestObject)xof.GetObject("rod");

            Assert.AreEqual(1, rod.Age);

            RootObjectDefinition def      = (RootObjectDefinition)xof.GetObjectDefinition("rod");
            ConstructorResolver  resolver = new ConstructorResolver(xof, xof, new SimpleInstantiationStrategy(),
                                                                    new ObjectDefinitionValueResolver(xof));

            ConstructorInstantiationInfo ci = resolver.GetConstructorInstantiationInfo("rod", def, null, null);

            AbstractObjectDefinition objDef = (AbstractObjectDefinition)xof.GetObjectDefinition("foo");

            objDef.IsAbstract = false;

            TestObject foo = (TestObject)xof.GetObject("foo");


            Assert.AreEqual(2, foo.Age);
        }
コード例 #3
0
            /// <summary>
            /// Applies attributes to the proxy class.
            /// </summary>
            /// <param name="typeBuilder">The type builder to use.</param>
            /// <param name="targetType">The proxied class.</param>
            /// <see cref="IProxyTypeBuilder.ProxyTargetAttributes"/>
            /// <see cref="IProxyTypeBuilder.TypeAttributes"/>
            protected override void ApplyTypeAttributes(TypeBuilder typeBuilder, Type targetType)
            {
                foreach (object attr in GetTypeAttributes(targetType))
                {
                    if (attr is CustomAttributeBuilder)
                    {
                        typeBuilder.SetCustomAttribute((CustomAttributeBuilder)attr);
                    }
                    else if (attr is CustomAttributeData)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((CustomAttributeData)attr));
                    }
                    else if (attr is Attribute)
                    {
                        typeBuilder.SetCustomAttribute(
                            ReflectionUtils.CreateCustomAttribute((Attribute)attr));
                    }
                    else if (attr is IObjectDefinition)
                    {
                        RootObjectDefinition objectDefinition = (RootObjectDefinition)attr;

                        //TODO check that object definition is for an Attribute type.

                        //Change object definition so it can be instantiated and make prototype scope.
                        objectDefinition.IsAbstract  = false;
                        objectDefinition.IsSingleton = false;
                        string objectName = ObjectDefinitionReaderUtils.GenerateObjectName(objectDefinition, objectFactory);
                        objectFactory.RegisterObjectDefinition(objectName, objectDefinition);


                        //find constructor and constructor arg values to create this attribute.
                        ConstructorResolver constructorResolver = new ConstructorResolver(objectFactory, objectFactory,
                                                                                          new SimpleInstantiationStrategy(),
                                                                                          new ObjectDefinitionValueResolver(objectFactory));


                        ConstructorInstantiationInfo ci = constructorResolver.GetConstructorInstantiationInfo(objectName,
                                                                                                              objectDefinition,
                                                                                                              null, null);

                        if (objectDefinition.PropertyValues.PropertyValues.Count == 0)
                        {
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo,
                                                                                    ci.ArgInstances);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                        else
                        {
                            object         attributeInstance        = objectFactory.GetObject(objectName);
                            IObjectWrapper wrappedAttributeInstance = new ObjectWrapper(attributeInstance);
                            PropertyInfo[] namedProperties          = wrappedAttributeInstance.GetPropertyInfos();
                            object[]       propertyValues           = new object[namedProperties.Length];
                            for (int i = 0; i < namedProperties.Length; i++)
                            {
                                propertyValues[i] =
                                    wrappedAttributeInstance.GetPropertyValue(namedProperties[i].Name);
                            }
                            CustomAttributeBuilder cab = new CustomAttributeBuilder(ci.ConstructorInfo, ci.ArgInstances,
                                                                                    namedProperties, propertyValues);
                            typeBuilder.SetCustomAttribute(cab);
                        }
                    }
                }
            }