Beispiel #1
0
        /// <summary>
        /// 装配字段
        /// </summary>
        /// <param name="property"></param>
        /// <param name="context"></param>
        /// <param name="Parameters"></param>
        /// <param name="instance"></param>
        /// <param name="allowCircle"></param>
        /// <returns></returns>
        public object ResolveField(FieldInfo property, IComponentContext context, IEnumerable <Parameter> Parameters, object instance, bool allowCircle)
        {
            if (!allowCircle)
            {
                return(property == null ? null : Resolve(property.DeclaringType, property.FieldType, context, "field"));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }


            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, property.FieldType);
            }
            else
            {
                propertyService = new TypedService(property.FieldType);
            }

            if (Parameters != null && Parameters.Count() == 1)
            {
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }
                if (AutowiredParmeter.AutowiredChains.TryGetValue(property.FieldType.FullName, out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    AutowiredParmeter.Add(property.DeclaringType.FullName, instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                instanceTypeParameter.Add(property.DeclaringType.FullName, instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }
            return(null);
        }
        internal object Resolve(IComponentContext context, object instance, Type classType, Type memberType, IEnumerable <Parameter> Parameters)
        {
            if ((typeof(IObjectFactory).IsAssignableFrom(memberType)))
            {
                return(context.Resolve <ObjectBeanFactory>().CreateAutowiredFactory(this, memberType, classType, instance, Parameters));
            }

            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, memberType);
            }
            else
            {
                propertyService = new TypedService(memberType);
            }

            // ReSharper disable once PossibleMultipleEnumeration
            if (Parameters != null && Parameters.Count() == 1)
            {
                // ReSharper disable once PossibleMultipleEnumeration
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }

                // ReSharper disable once AssignNullToNotNullAttribute
                if (AutowiredParmeter.TryGet(getAutowiredParmeterKey(memberType), out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    // ReSharper disable once PossibleNullReferenceException
                    AutowiredParmeter.TryAdd(getAutowiredParmeterKey(classType), instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                // ReSharper disable once PossibleNullReferenceException
                instanceTypeParameter.TryAdd(getAutowiredParmeterKey(classType), instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }

            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// 装配字段
        /// </summary>
        /// <param name="property"></param>
        /// <param name="context"></param>
        /// <param name="Parameters"></param>
        /// <param name="instance"></param>
        /// <param name="allowCircle"></param>
        /// <returns></returns>
        public object ResolveField(FieldInfo property, IComponentContext context, IEnumerable <Parameter> Parameters, object instance, bool allowCircle)
        {
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }
            if (!allowCircle)
            {
                return(Resolve(property.DeclaringType, property.FieldType, context, "field"));
            }
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }


            Service propertyService = null;

            if (!string.IsNullOrEmpty(this.Name))
            {
                propertyService = new KeyedService(this.Name, property.FieldType);
            }
            else
            {
                propertyService = new TypedService(property.FieldType);
            }

            // ReSharper disable once PossibleMultipleEnumeration
            if (Parameters != null && Parameters.Count() == 1)
            {
                // ReSharper disable once PossibleMultipleEnumeration
                if (!(Parameters.First() is AutowiredParmeter AutowiredParmeter))
                {
                    return(null);
                }
                // ReSharper disable once AssignNullToNotNullAttribute
                if (AutowiredParmeter.TryGet(getAutowiredParmeterKey(property.FieldType), out var objectInstance))
                {
                    return(objectInstance);
                }
                else
                {
                    // ReSharper disable once PossibleNullReferenceException
                    AutowiredParmeter.TryAdd(getAutowiredParmeterKey(property.DeclaringType), instance);
                    if (context.TryResolveService(propertyService, new Parameter[] { AutowiredParmeter }, out var propertyValue))
                    {
                        return(propertyValue);
                    }
                }
            }
            else
            {
                var instanceTypeParameter = new AutowiredParmeter();
                // ReSharper disable once PossibleNullReferenceException
                instanceTypeParameter.TryAdd(getAutowiredParmeterKey(property.DeclaringType), instance);
                if (context.TryResolveService(propertyService, new Parameter[] { instanceTypeParameter }, out var propertyValue))
                {
                    return(propertyValue);
                }
            }
            return(null);
        }