private ConstructorExpression BuildConstructorExpression(Type concreteType, DependencyChain chain) { // If the concrete type is a generic type definition, we cannot invoke it's constructor. // We need to construct the type using the generic arguments defined on the requested type from the dependency chain. if (concreteType.IsGenericTypeDefinition) { concreteType = concreteType.MakeGenericType(chain.Type.GetGenericArguments()); } // Order by constructors with the most amount of parameters. var constructors = concreteType.GetConstructors(); var expression = new ConstructorExpression(); // Don't bother ordering if only 1 constructor is present. if (constructors.Length == 1) { return(BuildConstructor(constructors[0], chain)); } var constructor = constructors .OrderByDescending(x => x.GetParameters().Length) .FirstOrDefault(); // No valid public constructors were found. if (constructor == null) { throw new MissingConstructorException(concreteType); } return(BuildConstructor(constructor, chain)); }
private ConstructorExpression BuildConstructor(ConstructorInfo constructor, DependencyChain chain) { var parameters = constructor.GetParameters(); var expression = new ConstructorExpression { ParameterChains = new DependencyChain[parameters.Length] }; // Iterate through all the parameters in the constructor. for (var i = 0; i < parameters.Length; i++) { // Create a new DependencyChain node for the parameter type. var childChain = new DependencyChain(parameters[i].ParameterType, chain); expression.ParameterChains[i] = childChain; } expression.BuildExpression(constructor); return(expression); }