Beispiel #1
0
        internal static object CreateInstance([NotNull] Type type, [NotNull] ResolutionFunction resolutionFunction)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (resolutionFunction == null)
            {
                throw new ArgumentNullException(nameof(resolutionFunction));
            }

            if (TryInstantiateViaBakedData(type, resolutionFunction, out var bakedDataInstance))
            {
                return(bakedDataInstance);
            }

            if (DiSettings.TryGetInstance(out var settings) && settings.UseBakedData)
            {
                object PocoResolutionFunction(Type resolvedType)
                {
                    if (resolutionFunction(resolvedType, out var resolvedObject))
                    {
                        return(resolvedObject);
                    }
                    throw new DependencyNotRegisteredException(resolvedType);
                }

                if (BakedInjection.TryInstantiate(type, PocoResolutionFunction, out var bakedInstance))
                {
                    return(bakedInstance);
                }
            }

            if (!TryGetInjectableConstructorParameters(type, out var parameters))
            {
                throw new ArgumentException($"Type {type} does not have an injectable constructor.");
            }

            var arguments = Injection.RentArgumentsArray(parameters.Count);

            for (var index = 0; index < parameters.Count; index++)
            {
                var parameterType = parameters[index].ParameterType;
                if (resolutionFunction(parameterType, out var dependency))
                {
                    arguments[index] = dependency;
                }
                else
                {
                    Injection.ReturnArgumentsArray(arguments);
                    throw new DependencyNotRegisteredException(parameterType);
                }
            }

            var instance = Activator.CreateInstance(type, arguments);

            Injection.ReturnArgumentsArray(arguments);
            return(instance);
        }
Beispiel #2
0
        public void Resolve()
        {
            _affectedComponents.Clear();

            var extraCondition = UseBakedData ? _bakedIsAffectedExtraCondition : null;
            var transform      = GameObject.transform;

            if (!Injection.TryGetAffectedComponentsFast(_affectedComponents, transform, extraCondition))
            {
                Injection.GetAffectedComponents(_affectedComponents, transform, extraCondition);
            }

            foreach (var(component, _) in _affectedComponents)
            {
                Inject(component);
            }

            _cache.Clear();
        }