Example #1
0
        object ResolveField(FieldInfo fieldInfo, ResolveContext context, ZenUtil.InjectInfo injectInfo)
        {
            var desiredType = fieldInfo.FieldType;
            var valueObj = _container.TryResolve(desiredType, context);

            if (valueObj == null)
            {
                // If it's a list it might map to a collection
                if (ReflectionUtil.IsGenericList(desiredType))
                {
                    var subTypes = desiredType.GetGenericArguments();

                    if (subTypes.Length == 1)
                    {
                        var subType = subTypes[0];

                        // Dependencies that are lists are only optional if declared as such using the inject attribute
                        bool optional = (injectInfo == null ? false : injectInfo.Optional);

                        valueObj = _container.ResolveMany(subType, context, optional);
                    }
                }
            }

            return valueObj;
        }
Example #2
0
        static IEnumerable <ZenjectResolveException> ValidateInstallers(CompositionRoot compRoot)
        {
            var container = new DiContainer();

            container.Bind <CompositionRoot>().ToSingle(compRoot);

            foreach (var installer in compRoot.Installers)
            {
                if (installer == null)
                {
                    yield return(new ZenjectResolveException(
                                     "Found null installer in properties of Composition Root"));

                    yield break;
                }

                installer.Container = container;
                container.Bind <IInstaller>().To(installer);
            }

            foreach (var error in ZenUtil.ValidateInstallers(container))
            {
                yield return(error);
            }

            // Also make sure we can fill in all the dependencies in the built-in scene
            foreach (var monoBehaviour in compRoot.GetComponentsInChildren <MonoBehaviour>())
            {
                if (monoBehaviour == null)
                {
                    // Be nice to give more information here
                    Debug.LogWarning("Found null MonoBehaviour in scene");
                    continue;
                }

                foreach (var error in container.ValidateObjectGraph(monoBehaviour.GetType()))
                {
                    yield return(error);
                }
            }
        }
Example #3
0
        void Register()
        {
            if (Installers.IsEmpty())
            {
                Debug.LogError("No installers found while initializing CompositionRoot");
                return;
            }

            foreach (var installer in Installers)
            {
                // 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
                FieldsInjecter.Inject(_container, installer);
                _container.Bind <IInstaller>().To(installer);
            }

            ZenUtil.InstallInstallers(_container);
        }
Example #4
0
 public static void LoadScene(string levelName)
 {
     ZenUtil.LoadScene(levelName, null);
 }