コード例 #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));
        }
コード例 #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));
        }
コード例 #3
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));
        }