public static void InstallSceneInstallers(
            DiContainer container, IEnumerable <IInstaller> installers)
        {
            foreach (var installer in installers)
            {
                if (installer == null)
                {
                    Log.Warn("Found null installer in composition root");
                    continue;
                }

                if (installer.IsEnabled)
                {
                    // The installers that are part of the scene are monobehaviours
                    // and therefore were not created via Zenject and therefore do
                    // not have their members injected
                    // At the very least they will need the container injected but
                    // they might also have some configuration passed from another
                    // scene as well
                    FieldsInjecter.Inject(container, installer);
                    container.Bind <IInstaller>().To(installer);

                    // Install this installer and also any other installers that it installs
                    container.InstallInstallers();

                    Assert.That(!container.HasBinding <IInstaller>());
                }
            }
        }
 public static void InjectMonoBehaviour(
     DiContainer container, Component component, IEnumerable <object> extraArgs)
 {
     // null if monobehaviour link is broken
     if (component != null)
     {
         using (container.PushLookup(component.GetType()))
         {
             FieldsInjecter.Inject(container, component, extraArgs);
         }
     }
 }
Example #3
0
        object InstantiateInternal(
            Type concreteType, IEnumerable <TypeValuePair> extraArgMapParam)
        {
            Assert.That(!concreteType.DerivesFrom <UnityEngine.Component>(),
                        "Error occurred while instantiating object of type '{0}'. Instantiator should not be used to create new mono behaviours.  Must use GameObjectInstantiator, GameObjectFactory, or GameObject.Instantiate.", concreteType.Name());

            var typeInfo = TypeAnalyzer.GetInfo(concreteType);

            if (typeInfo.InjectConstructor == null)
            {
                throw new ZenjectResolveException(
                          "More than one or zero constructors found for type '{0}' when creating dependencies.  Use one [Inject] attribute to specify which to use.".With(concreteType));
            }

            // Make a copy since we remove from it below
            var extraArgMap = extraArgMapParam.ToList();
            var paramValues = new List <object>();

            foreach (var injectInfo in typeInfo.ConstructorInjectables)
            {
                object value;

                if (!InstantiateUtil.PopValueWithType(extraArgMap, injectInfo.ContractType, out value))
                {
                    value = _container.Resolve(injectInfo);
                }

                paramValues.Add(value);
            }

            object newObj;

            try
            {
                using (ProfileBlock.Start("{0}.{0}()", concreteType))
                {
                    newObj = typeInfo.InjectConstructor.Invoke(paramValues.ToArray());
                }
            }
            catch (Exception e)
            {
                throw new ZenjectResolveException(
                          "Error occurred while instantiating object with type '{0}'".With(concreteType.Name()), e);
            }

            FieldsInjecter.Inject(_container, newObj, extraArgMap, true, typeInfo);

            return(newObj);
        }