Exemple #1
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType)
        {
            var injectAttr = memInfo.Attribute <InjectAttribute>();

            var info = new InjectableInfo()
            {
                Optional      = memInfo.HasAttribute(typeof(InjectOptionalAttribute)),
                Identifier    = (injectAttr == null ? null : injectAttr.Identifier),
                SourceName    = memInfo.Name,
                EnclosingType = enclosingType,
            };

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                info.Setter       = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                info.ContractType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                info.Setter       = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                info.ContractType = propInfo.PropertyType;
            }

            return(info);
        }
Exemple #2
0
        static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType)
        {
            var injectAttr = memInfo.Attribute<InjectAttribute>();

            var info = new InjectableInfo()
            {
                Optional = memInfo.HasAttribute(typeof(InjectOptionalAttribute)),
                Identifier = (injectAttr == null ? null : injectAttr.Identifier),
                SourceName = memInfo.Name,
                EnclosingType = enclosingType,
            };

            if (memInfo is FieldInfo)
            {
                var fieldInfo = (FieldInfo)memInfo;
                info.Setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value));
                info.ContractType = fieldInfo.FieldType;
            }
            else
            {
                Assert.That(memInfo is PropertyInfo);
                var propInfo = (PropertyInfo)memInfo;
                info.Setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null));
                info.ContractType = propInfo.PropertyType;
            }

            return info;
        }
Exemple #3
0
        internal object Resolve(
            InjectableInfo injectInfo, object targetInstance)
        {
            var context = new ResolveContext(
                injectInfo, LookupsInProgress.ToList(), targetInstance);

            return(Resolve(injectInfo.ContractType, context, injectInfo.Optional));
        }
 internal ResolveContext(
     InjectableInfo injectInfo, List <Type> parents, object targetInstance)
 {
     Identifier        = injectInfo.Identifier;
     SourceName        = injectInfo.SourceName;
     EnclosingType     = injectInfo.EnclosingType;
     EnclosingInstance = targetInstance;
     ParentTypes       = parents;
 }
Exemple #5
0
 internal InjectContext(
     InjectableInfo injectInfo, List<Type> parents, object targetInstance)
 {
     Optional = injectInfo.Optional;
     Identifier = injectInfo.Identifier;
     SourceName = injectInfo.SourceName;
     EnclosingType = injectInfo.EnclosingType;
     EnclosingInstance = targetInstance;
     ParentTypes = parents;
 }
Exemple #6
0
 internal InjectContext(
     DiContainer container,
     InjectableInfo injectInfo, List <Type> parents, object targetInstance)
 {
     Container         = container;
     Optional          = injectInfo.Optional;
     Identifier        = injectInfo.Identifier;
     SourceName        = injectInfo.SourceName;
     EnclosingType     = injectInfo.EnclosingType;
     EnclosingInstance = targetInstance;
     ParentTypes       = parents;
 }
        static void InjectFromResolve(
            InjectableInfo injectInfo, DiContainer container, object targetInstance)
        {
            var valueObj = container.Resolve(injectInfo, targetInstance);

            if (valueObj == null && !container.AllowNullBindings)
            {
                // Do not change if optional
                // Since it may have some hard coded value
                Assert.That(injectInfo.Optional); // Should have thrown resolve exception otherwise
            }
            else
            {
                Assert.IsNotNull(injectInfo.Setter);
                injectInfo.Setter(targetInstance, valueObj);
            }
        }
Exemple #8
0
        static void InjectFromResolve(
            InjectableInfo injectInfo, DiContainer container, object targetInstance)
        {
            var valueObj = container.Resolve(injectInfo, targetInstance);

            if (valueObj == null && !container.AllowNullBindings)
            {
                // Do not change if optional
                // Since it may have some hard coded value
                Assert.That(injectInfo.Optional); // Should have thrown resolve exception otherwise
            }
            else
            {
                Assert.IsNotNull(injectInfo.Setter);
                injectInfo.Setter(targetInstance, valueObj);
            }
        }
        static bool InjectFromExtras(
            InjectableInfo injectInfo,
            object injectable, List<object> additional)
        {
            foreach (object obj in additional)
            {
                if (injectInfo.ContractType.IsAssignableFrom(obj.GetType()))
                {
                    Assert.IsNotNull(injectInfo.Setter);

                    injectInfo.Setter(injectable, obj);
                    additional.Remove(obj);
                    return true;
                }
            }

            return false;
        }
Exemple #10
0
        static bool InjectFromExtras(
            InjectableInfo injectInfo,
            object injectable, List <object> additional)
        {
            foreach (object obj in additional)
            {
                if (injectInfo.ContractType.IsAssignableFrom(obj.GetType()))
                {
                    Assert.IsNotNull(injectInfo.Setter);

                    injectInfo.Setter(injectable, obj);
                    additional.Remove(obj);
                    return(true);
                }
            }

            return(false);
        }
Exemple #11
0
        object ResolveFromType(
            ResolveContext context, object injectable, InjectableInfo injectInfo)
        {
            if (_container.HasBinding(injectInfo.ContractType, context))
            {
                return(_container.Resolve(injectInfo.ContractType, context));
            }

            // If it's a list it might map to a collection
            if (ReflectionUtil.IsGenericList(injectInfo.ContractType))
            {
                var subType = injectInfo.ContractType.GetGenericArguments().Single();
                return(_container.ResolveMany(subType, context, injectInfo.Optional));
            }

            if (!injectInfo.Optional)
            {
                throw new ZenjectResolveException(
                          "Unable to find field with type '{0}' when injecting dependencies into '{1}'. \nObject graph:\n {2}"
                          .With(injectInfo.ContractType, injectable, _container.GetCurrentObjectGraph()));
            }

            return(null);
        }
Exemple #12
0
 internal object Resolve(InjectableInfo injectInfo)
 {
     return(Resolve(injectInfo, null));
 }