Exemplo n.º 1
0
        public static ConfiguredLambdaInfo For(LambdaExpression lambda)
        {
            var funcArguments    = lambda.Parameters.ProjectToArray(p => p.Type);
            var contextTypes     = GetContextTypes(funcArguments);
            var parameterSwapper = ParametersSwapper.For(contextTypes, funcArguments);

            return(new ConfiguredLambdaInfo(lambda, contextTypes, lambda.ReturnType, parameterSwapper));
        }
Exemplo n.º 2
0
        public static ConfiguredLambdaInfo For(LambdaExpression lambda)
        {
            var funcArguments    = lambda.Parameters.Select(p => p.Type).ToArray();
            var contextTypes     = (funcArguments.Length != 1) ? funcArguments : funcArguments[0].GetGenericArguments();
            var parameterSwapper = ParametersSwapper.For(contextTypes, funcArguments);

            return(new ConfiguredLambdaInfo(lambda, lambda.ReturnType, parameterSwapper));
        }
Exemplo n.º 3
0
 private ConfiguredLambdaInfo(
     LambdaExpression lambda,
     Type returnType,
     ParametersSwapper parametersSwapper)
 {
     _lambda            = lambda;
     _parametersSwapper = parametersSwapper;
     ReturnType         = returnType;
 }
Exemplo n.º 4
0
        private ConfiguredLambdaInfo(
            LambdaExpression lambda,
            Type[] contextTypes,
            Type returnType,
            ParametersSwapper parametersSwapper)
        {
            _lambda            = lambda;
            _contextTypes      = contextTypes;
            _parametersSwapper = parametersSwapper;
            ReturnType         = returnType;

            _isForTargetDictionary = (contextTypes.Length > 1) && contextTypes[1].IsDictionary();
        }
Exemplo n.º 5
0
        private static ConfiguredLambdaInfo For <T>(
            T func,
            Type[] contextTypes,
            Func <Type[], Type[]> funcArgumentsFactory,
            Func <Type[], Type> returnTypeFactory,
            params Type[] allowedTypes)
        {
            var funcType = typeof(T);

            if (!funcType.IsGenericType())
            {
                return(null);
            }

            var funcTypeDefinition = funcType.GetGenericTypeDefinition();

            if (!allowedTypes.Contains(funcTypeDefinition))
            {
                return(null);
            }

            var funcTypes        = funcType.GetGenericTypeArguments();
            var funcArguments    = funcArgumentsFactory.Invoke(funcTypes);
            var parameterSwapper = ParametersSwapper.For(contextTypes, funcArguments);

            if (parameterSwapper == null)
            {
                return(null);
            }

            var parameters             = funcArguments.ProjectToArray(Parameters.Create);
            var valueFactory           = func.ToConstantExpression();
            var valueFactoryInvocation = Expression.Invoke(valueFactory, parameters.Cast <Expression>());
            var valueFactoryLambda     = Expression.Lambda(funcType, valueFactoryInvocation, parameters);

            return(new ConfiguredLambdaInfo(
                       valueFactoryLambda,
                       contextTypes,
                       returnTypeFactory.Invoke(funcTypes),
                       parameterSwapper));
        }