Beispiel #1
0
        public static ConfiguredLambdaInfo For(LambdaExpression lambda, MappingConfigInfo configInfo)
        {
            var funcArguments        = lambda.Parameters.ProjectToArray(p => p.Type);
            var contextTypes         = GetContextTypes(funcArguments);
            var valueInjectorFactory = ValueInjectorFactory.For(contextTypes, funcArguments);

            return(new ConfiguredLambdaInfo(
                       lambda,
                       contextTypes,
                       lambda.ReturnType,
                       valueInjectorFactory,
                       configInfo));
        }
Beispiel #2
0
        private ConfiguredLambdaInfo(
            LambdaExpression lambda,
            Type[] contextTypes,
            Type returnType,
            ValueInjectorFactory valueInjectorFactory,
            MappingConfigInfo configInfo)
        {
            _lambda        = lambda;
            _lambdaBody    = lambda.Body;
            _contextTypes  = contextTypes;
            _valueInjector = valueInjectorFactory.CreateFor(lambda, configInfo);
            ReturnType     = returnType;

            _isForTargetDictionary = (contextTypes.Length > 1) && contextTypes[1].IsDictionary();
        }
Beispiel #3
0
        private static ConfiguredLambdaInfo For <T>(
            T func,
            MappingConfigInfo configInfo,
            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 valueInjectorFactory = ValueInjectorFactory.For(contextTypes, funcArguments);

            if (valueInjectorFactory == 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),
                       valueInjectorFactory,
                       configInfo));
        }