private static ParameterValue BuildDependencyParameter(Expression arg) { MethodCallExpression methodCallExpression = arg as MethodCallExpression; if (methodCallExpression != null && methodCallExpression.Method.DeclaringType == typeof(Container)) { if (Container.IsResolved(methodCallExpression.Method)) { return(new ContainerResolvedParameter(methodCallExpression)); } if (Container.IsResolvedEnumerable(methodCallExpression.Method)) { return(new ContainerResolvedEnumerableParameter(methodCallExpression)); } if (Container.IsOptionalResolved(methodCallExpression.Method)) { if (string.IsNullOrEmpty(Container.CalculateNameForMethodCall(methodCallExpression))) { return(new ConstantParameterValue(Expression.Constant(null, methodCallExpression.Type))); } else { return(new ContainerResolvedParameter(methodCallExpression)); } } throw new ArgumentException(Properties.Resources.ExceptionUnrecognizedContainerMarkerMethod); } return(new ConstantParameterValue(arg)); }
internal ContainerResolvedEnumerableParameter(MethodCallExpression expression) : base(expression) { IEnumerable <string> nameListArgument = Expression.Lambda(expression.Arguments[0]).Compile().DynamicInvoke() as IEnumerable <string>; if (nameListArgument == null) { throw new ArgumentNullException(Container.CalculateNameForMethodCall(expression)); } Names = nameListArgument; ElementType = expression.Type.GetGenericArguments()[0]; }
/// <summary> /// Initializes the construction parameter from the <see cref="MethodCallExpression"/>. This method call expression /// expected to be respresented through the <see cref="Container"/> static marker class. /// </summary> /// <remarks> /// /// Given a class Example defined as: /// /// public class Example /// { /// public Example(Argument arg); /// } /// /// A <see cref="TypeRegistration{T}"/> and <see cref="LambdaExpression"/> for this configuration might appear as follows: /// new TypeRegistration<Example>(() => new Example(Container.Resolved<Argument>("SomeName")); /// /// During construction of the Example class, Argument will be resolved and injected by the container. /// The <see cref="Container.Resolved{T}()"/> marker interface is used to represent /// this requirement to a container configurator and is translated to a <see cref="ContainerResolvedParameter"/>. /// </remarks> /// <seealso cref="Container"/> /// <param name="expression">The method expression representing the type to resolve and named value.</param> internal ContainerResolvedParameter(MethodCallExpression expression) : base(expression) { Name = Container.CalculateNameForMethodCall(expression); }