コード例 #1
0
        /// <summary>
        /// Determines whether the specified object qualifies as an autowire candidate,
        /// to be injected into other beans which declare a dependency of matching type.
        /// This method checks ancestor factories as well.
        /// </summary>
        /// <param name="objectName">Name of the object to check.</param>
        /// <param name="descriptor">The descriptor of the dependency to resolve.</param>
        /// <returns>
        ///     <c>true</c> if the object should be considered as an autowire candidate; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="NoSuchObjectDefinitionException">if there is no object with the given name.</exception>
        public bool IsAutowireCandidate(string objectName, DependencyDescriptor descriptor)
        {
            //Consider FactoryObjects as autowiring candidates.
            bool isFactoryObject = (descriptor != null && descriptor.DependencyType != null &&
                                    typeof(IFactoryObject).IsAssignableFrom(descriptor.DependencyType));

            if (isFactoryObject)
            {
                objectName = ObjectFactoryUtils.TransformedObjectName(objectName);
            }

            if (!ContainsObjectDefinition(objectName))
            {
                if (ContainsSingleton(objectName))
                {
                    return(true);
                }
                else if (ParentObjectFactory is IConfigurableListableObjectFactory)
                {
                    // No object definition found in this factory -> delegate to parent
                    return
                        (((IConfigurableListableObjectFactory)ParentObjectFactory).IsAutowireCandidate(objectName, descriptor));
                }
            }
            return(IsAutowireCandidate(objectName, GetMergedObjectDefinition(objectName, true), descriptor));
        }
コード例 #2
0
        /// <summary>
        /// Determine the type of the object with the given name.
        /// </summary>
        /// <remarks>
        /// <p>
        /// More specifically, checks the type of object that
        /// <see cref="Spring.Objects.Factory.IObjectFactory.GetObject(string)"/> would return.
        /// For an <see cref="Spring.Objects.Factory.IFactoryObject"/>, returns the type
        /// of object that the <see cref="Spring.Objects.Factory.IFactoryObject"/> creates.
        /// </p>
        /// </remarks>
        /// <param name="name">The name of the object to query.</param>
        /// <returns>
        /// The <see cref="System.Type"/> of the object or <see langword="null"/> if
        /// not determinable.
        /// </returns>
        public Type GetType(string name)
        {
            string objectName = ObjectFactoryUtils.TransformedObjectName(name);
            object instance   = objects[objectName];

            if (instance == null)
            {
                throw new NoSuchObjectDefinitionException(name, GrabDefinedObjectsString());
            }
            if (instance is IFactoryObject && !ObjectFactoryUtils.IsFactoryDereference(name))
            {
                return(((IFactoryObject)instance).ObjectType);
            }
            return(instance.GetType());
        }