Example #1
0
        internal static void Inject(
            DiContainer container, object injectable,
            IEnumerable <object> additional, bool shouldUseAll, ZenjectTypeInfo typeInfo)
        {
            Assert.That(!additional.Contains(null),
                        "Null value given to injection argument list. In order to use null you must provide a List<TypeValuePair> and not just a list of objects");

            Inject(
                container, injectable,
                InstantiateUtil.CreateTypeValueList(additional), shouldUseAll, typeInfo);
        }
Example #2
0
        internal static void Inject(
            DiContainer container, object injectable,
            IEnumerable <TypeValuePair> extraArgMapParam, bool shouldUseAll, ZenjectTypeInfo typeInfo)
        {
            Assert.IsEqual(typeInfo.TypeAnalyzed, injectable.GetType());
            Assert.That(injectable != null);

            // Make a copy since we remove from it below
            var extraArgMap = extraArgMapParam.ToList();

            foreach (var injectInfo in typeInfo.FieldInjectables.Concat(typeInfo.PropertyInjectables))
            {
                object value;

                if (InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.ContractType, out value))
                {
                    injectInfo.Setter(injectable, value);
                }
                else
                {
                    value = container.Resolve(injectInfo, injectable);

                    if (injectInfo.Optional && value == null)
                    {
                        // Do not override in this case so it retains the hard-coded value
                    }
                    else
                    {
                        injectInfo.Setter(injectable, value);
                    }
                }
            }

            if (shouldUseAll && !extraArgMap.IsEmpty())
            {
                throw new ZenjectResolveException(
                          "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}"
                          .With(injectable.GetType().Name(), String.Join(",", extraArgMap.Select(x => x.Type.Name()).ToArray()), DiContainer.GetCurrentObjectGraph()));
            }

            foreach (var methodInfo in typeInfo.PostInjectMethods)
            {
                using (ProfileBlock.Start("{0}.{1}()", injectable.GetType(), methodInfo.Name))
                {
                    methodInfo.Invoke(injectable, new object[0]);
                }
            }
        }
        DiContainer CreateTempContainer(List <TypeValuePair> args)
        {
            DiContainer tempSubContainer = Container.CreateSubContainer();

            ZenjectTypeInfo installerInjectables = TypeAnalyzer.GetInfo(_installerType);

            foreach (TypeValuePair argPair in args)
            {
                // We need to intelligently match on the exact parameters here to avoid the issue
                // brought up in github issue #217
                InjectableInfo match = installerInjectables.AllInjectables
                                       .Where(x => argPair.Type.DerivesFromOrEqual(x.MemberType))
                                       .OrderBy(x => ZenUtilInternal.GetInheritanceDelta(argPair.Type, x.MemberType)).FirstOrDefault();

                Assert.That(match != null,
                            "Could not find match for argument type '{0}' when injecting into sub container installer '{1}'",
                            argPair.Type, _installerType);

                tempSubContainer.Bind(match.MemberType)
                .FromInstance(argPair.Value).WhenInjectedInto(_installerType);
            }

            return(tempSubContainer);
        }
Example #4
0
        // Iterate over fields/properties on the given object and inject any with the [Inject] attribute
        internal void Inject(
            object injectable, IEnumerable <TypeValuePair> extraArgMapParam,
            bool shouldUseAll, ZenjectTypeInfo typeInfo, InjectContext context)
        {
            Assert.IsEqual(typeInfo.TypeAnalyzed, injectable.GetType());
            Assert.That(injectable != null);

#if !ZEN_NOT_UNITY3D
            Assert.That(injectable.GetType() != typeof(GameObject),
                        "Use InjectGameObject to Inject game objects instead of Inject method");
#endif

            // Make a copy since we remove from it below
            var extraArgMap = extraArgMapParam.ToList();

            foreach (var injectInfo in typeInfo.FieldInjectables.Concat(typeInfo.PropertyInjectables))
            {
                object value;

                if (InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.MemberType, out value))
                {
                    injectInfo.Setter(injectable, value);
                }
                else
                {
                    value = Resolve(
                        injectInfo.CreateInjectContext(this, context, injectable));

                    if (injectInfo.Optional && value == null)
                    {
                        // Do not override in this case so it retains the hard-coded value
                    }
                    else
                    {
                        injectInfo.Setter(injectable, value);
                    }
                }
            }

            foreach (var method in typeInfo.PostInjectMethods)
            {
                using (ProfileBlock.Start("{0}.{1}()", injectable.GetType(), method.MethodInfo.Name))
                {
                    var paramValues = new List <object>();

                    foreach (var injectInfo in method.InjectableInfo)
                    {
                        object value;

                        if (!InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.MemberType, out value))
                        {
                            value = Resolve(
                                injectInfo.CreateInjectContext(this, context, injectable));
                        }

                        paramValues.Add(value);
                    }

                    method.MethodInfo.Invoke(injectable, paramValues.ToArray());
                }
            }

            if (shouldUseAll && !extraArgMap.IsEmpty())
            {
                throw new ZenjectResolveException(
                          "Passed unnecessary parameters when injecting into type '{0}'. \nExtra Parameters: {1}\nObject graph:\n{2}"
                          .Fmt(injectable.GetType().Name(), String.Join(",", extraArgMap.Select(x => x.Type.Name()).ToArray()), context.GetObjectGraphString()));
            }
        }