Exemplo n.º 1
0
        public ResolveInfo Search(TypeKey requestedTypeKey, DependencyInjectionContainer container, bool?tryResolveAll)
        {
            requestedTypeKey.MustNotBeEmpty(nameof(requestedTypeKey));
            container.MustNotBeNull(nameof(container));

            if (tryResolveAll == false || requestedTypeKey.RegistrationName != string.Empty)
            {
                return(FindRegistration(requestedTypeKey, container));
            }

            var closedConstructedIEnumerableType = requestedTypeKey.Type.FindClosedConstructedIEnumerableType();

            if (tryResolveAll == true && closedConstructedIEnumerableType != null)
            {
                return(FindAllRegistrations(requestedTypeKey.Type, GetItemTypeOfCollection(closedConstructedIEnumerableType, requestedTypeKey), container));
            }
            if (closedConstructedIEnumerableType == null)
            {
                return(FindRegistration(requestedTypeKey, container));
            }

            var existingRegistration = container.GetRegistration(requestedTypeKey);

            if (existingRegistration != null && existingRegistration.IsGenericRegistration == false)
            {
                return(new ResolveRegistrationInfo(requestedTypeKey, existingRegistration));
            }

            return(FindForCollectionType(requestedTypeKey, container, closedConstructedIEnumerableType, existingRegistration));
        }
Exemplo n.º 2
0
        private static ResolveRegistrationInfo FindRegistration(TypeKey requestedTypeKey, DependencyInjectionContainer container)
        {
            var registration = container.GetRegistration(requestedTypeKey);

            if (registration != null)
            {
                return(new ResolveRegistrationInfo(requestedTypeKey, registration));
            }

            throw new ResolveException($"There is no registration present for type {requestedTypeKey}.");
        }
Exemplo n.º 3
0
        private Expression CreateResolveAllExpressionRecursively(ResolveAllInfo resolveAllInfo, DependencyOverrides dependencyOverrides, DependencyInjectionContainer container)
        {
            // Create the expression that instantiates the target collection
            var collectionRegistration = container.GetRegistration(resolveAllInfo.CollectionType);

            if (collectionRegistration == null)
            {
                throw new ResolveException($"There is no registration present to resolve collection type \"{resolveAllInfo.CollectionType}\".");
            }
            var createCollectionExpression = CreateConstructionExpression(new ResolveExpressionContext(new TypeKey(resolveAllInfo.CollectionType), collectionRegistration, dependencyOverrides, container));

            // Create the expression block that assigns the created collection to a variable, casts this variable to IList<TargetType>, and then resolves all registrations, adding the resulting the expressions
            var blockExpressions         = new Expression[resolveAllInfo.Registrations.Count + 3]; // +3 for initial assignment, casting to IList<ItemType>, and return statement
            var variableExpression       = Expression.Variable(resolveAllInfo.CollectionType);
            var assignVariableExpression = Expression.Assign(variableExpression, createCollectionExpression);

            blockExpressions[0] = assignVariableExpression; // Assign created collection to variable
            var closedConstructedICollectionType    = Constants.ICollectionGenericTypeDefinition.MakeGenericType(resolveAllInfo.ItemType);
            var castedICollectionVariableExpression = Expression.Variable(closedConstructedICollectionType);
            var assignCastedListExpression          = Expression.Assign(castedICollectionVariableExpression, Expression.ConvertChecked(variableExpression, closedConstructedICollectionType));

            blockExpressions[1] = assignCastedListExpression; // Assign casted list to variable

            // Resolve all registrations and add them to the collection
            var addMethodInfo = closedConstructedICollectionType.GetRuntimeMethod("Add", new[] { resolveAllInfo.ItemType });

            for (var i = 0; i < resolveAllInfo.Registrations.Count; i++)
            {
                var registration = resolveAllInfo.Registrations[i];
                var itemTypeKey  = new TypeKey(resolveAllInfo.ItemType, registration.RegistrationName);
                var resolveRegistrationExpression = CreateResolveExpressionRecursively(itemTypeKey, registration, dependencyOverrides, container);
                blockExpressions[i + 2] = Expression.Call(castedICollectionVariableExpression, addMethodInfo, resolveRegistrationExpression);
            }

            blockExpressions[blockExpressions.Length - 1] = variableExpression; // Return statement

            return(Expression.Block(resolveAllInfo.CollectionType, new[] { variableExpression, castedICollectionVariableExpression }, blockExpressions));
        }