Beispiel #1
0
        public void AddDependency(DependencyReference dependency)
        {
            if (dependency.Instance == null)
            {
                throw new ArgumentNullException("Dependency '" + dependency.Name + "' cannot be null");
            }

            _depsByString.Add(dependency.Name, dependency);

            // Fill the container with each possible type of the
            // dependency, i.g. the instance type and all of its parents.
            foreach (var type in dependency.Instance.GetType().GetAllTypes())
            {
                IList <DependencyReference> deps;
                if (!_depsByType.TryGetValue(type, out deps))
                {
                    deps = new List <DependencyReference>();
                    _depsByType.Add(type, deps);
                }
                deps.Add(dependency);
            }
        }
        private static bool InjectInternal(InjectionPoint injectionPoint, object subject, DependencyReference dependency,
                                           bool overrideExisting)
        {
            var isNameMatch = injectionPoint.Info.Name == null ||
                              injectionPoint.Info.Name.Equals(dependency.Name);
            var isTypeMatch = injectionPoint.Injector.Type.IsInstanceOfType(dependency.Instance) &&
                              injectionPoint.Injector.Info.DeclaringType.IsInstanceOfType(subject);
            var  currentValue = injectionPoint.Injector.GetValue(subject);
            bool isDependencyAlreadySet;

            if (currentValue is UnityEngine.Object)
            {
                isDependencyAlreadySet = (currentValue as UnityEngine.Object) != null;
            }
            else
            {
                isDependencyAlreadySet = currentValue != null;
            }

            if (isNameMatch && isTypeMatch && (!isDependencyAlreadySet || overrideExisting))
            {
                injectionPoint.Injector.SetValue(subject, dependency.Instance);
                return(true);
            }
            return(false);
        }