public static void SetComponents(ZenjectBinding binding, Component[] components)
        {
            var type  = typeof(ZenjectBinding);
            var field = type.GetField
                        (
                "_components",
                BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance
                        );

            Debug.Assert(field != null);
            field.SetValue(binding, components);
        }
Beispiel #2
0
        void InstallZenjectBinding(ZenjectBinding binding)
        {
            if (!binding.enabled)
            {
                return;
            }

            if (binding.Components == null || binding.Components.IsEmpty())
            {
                Log.Warn("Found empty list of components on ZenjectBinding on object '{0}'", binding.name);
                return;
            }

            string identifier = null;

            if (binding.Identifier.Trim().Length > 0)
            {
                identifier = binding.Identifier;
            }

            foreach (var component in binding.Components)
            {
                var bindType = binding.BindType;

                if (component == null)
                {
                    Log.Warn("Found null component in ZenjectBinding on object '{0}'", binding.name);
                    continue;
                }

                var componentType = component.GetType();

                switch (bindType)
                {
                case ZenjectBinding.BindTypes.Self:
                {
                    Container.Bind(componentType).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.BaseType:
                {
                    Container.Bind(componentType.BaseType()).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfaces:
                {
                    Container.Bind(componentType.Interfaces()).WithId(identifier).FromInstance(component);
                    break;
                }

                case ZenjectBinding.BindTypes.AllInterfacesAndSelf:
                {
                    Container.Bind(componentType.Interfaces().Concat(new[] { componentType }).ToArray()).WithId(identifier).FromInstance(component);
                    break;
                }

                default:
                {
                    throw Assert.CreateException();
                }
                }
            }
        }