Exemplo n.º 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);
        }
Exemplo n.º 2
0
        private static bool TryInstantiateViaBakedData([NotNull] Type type,
                                                       [NotNull] ResolutionFunction resolutionFunction, out object instance)
        {
            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 instance))
                {
                    return(true);
                }
            }

            instance = default;
            return(false);
        }