Exemple #1
0
        private Expression BuildLambdaExpression(ILambdaExpressionNode lambdaExpression)
        {
            if (_lambdaParameter == null)
            {
                throw BindingExceptionManager.UnexpectedExpressionNode(lambdaExpression);
            }

            var method = _lambdaParameter.ParameterType.GetMethodEx("Invoke", MemberFlags.Instance | MemberFlags.Public);

            if (method == null)
            {
                throw BindingExceptionManager.UnexpectedExpressionNode(lambdaExpression);
            }

            var parameters       = method.GetParameters();
            var lambdaParameters = new ParameterExpression[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                lambdaParameters[i] = Expression.Parameter(parameters[i].ParameterType, lambdaExpression.Parameters[i]);
            }

            for (int index = 0; index < lambdaParameters.Length; index++)
            {
                var parameter = lambdaParameters[index];
                _lambdaParameters.Add(parameter.Name, parameter);
            }
            var expression = BuildExpression(lambdaExpression.Expression);

            for (int index = 0; index < lambdaParameters.Length; index++)
            {
                _lambdaParameters.Remove(lambdaParameters[index].Name);
            }
            return(ExpressionReflectionManager.CreateLambdaExpression(expression, lambdaParameters));
        }
Exemple #2
0
 internal static bool TryGetStaticValue <T>(this Expression expression, out T value, bool throwOnError)
 {
     try
     {
         object v;
         var    exp = expression as ConstantExpression;
         if (exp == null)
         {
             v = ExpressionReflectionManager
                 .CreateLambdaExpression(expression, Empty.Array <ParameterExpression>())
                 .Compile()
                 .DynamicInvoke(Empty.Array <object>());
         }
         else
         {
             v = exp.Value;
         }
         value = (T)BindingServiceProvider.ValueConverter(BindingMemberInfo.Empty, typeof(T), v);
         return(true);
     }
     catch (Exception e)
     {
         value = default(T);
         if (throwOnError)
         {
             throw ExceptionManager.ExpressionShouldBeStaticValue(expression, e);
         }
         return(false);
     }
 }
        private Func <IDataContext, TType, object> Compile <TType>(Expression multiExpression, bool withContext)
            where TType : IList <object>
        {
            var parameters = new ParameterExpression[_members.Count + 1];

            parameters[0] = ContextExpression;
            for (int i = 0; i < _members.Count; i++)
            {
                parameters[i + 1] = _members[i].Key;
            }
            var @delegate = ExpressionReflectionManager
                            .CreateLambdaExpression(multiExpression, parameters)
                            .Compile();
            Func <object[], object> exp;
            var methodInfo = @delegate.GetType().GetMethodEx("Invoke", MemberFlags.Public | MemberFlags.Instance);

            if (methodInfo == null)
            {
                exp = @delegate.DynamicInvoke;
            }
            else
            {
                Func <object, object[], object> invokeMethod = ServiceProvider.ReflectionManager.GetMethodDelegate(methodInfo);
                exp = objects => invokeMethod(@delegate, objects);
            }
            if (withContext)
            {
                return(exp.AsBindingExpressionWithContext <TType>);
            }
            return(exp.AsBindingExpression <TType>);
        }
Exemple #4
0
        protected virtual Func <object[], object> CreateDelegate()
        {
            var expression = BuildExpression(_node);
            var @delegate  = ExpressionReflectionManager
                             .CreateLambdaExpression(expression, Parameters.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToArray())
                             .Compile();
            var methodInfo = @delegate.GetType().GetMethodEx("Invoke", MemberFlags.Public | MemberFlags.Instance);

            if (methodInfo == null)
            {
                return(@delegate.DynamicInvoke);
            }

            var invokeMethod = ServiceProvider.ReflectionManager.GetMethodDelegate(methodInfo);

            return(objects => invokeMethod(@delegate, objects));
        }