/// <summary>Resolve dictionary parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List<ParameterExpression> ResolveParameterDictionary(ExpressionScope scope, IDictionary<string, Type> parameterTypes)
        {
            var parameters = new List<ParameterExpression>();

            var parameterDictionary = scope.CreateParameter(typeof (IDictionary));
            parameters.Add(parameterDictionary);

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy<Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    Expression innerExpression = Expression.Property(parameterDictionary, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                        Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                        Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return innerParameter;
                }));
            }

            return parameters;
        }
        /// <summary>Resolve dictionary parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterDictionary(ExpressionScope scope, IDictionary <string, Type> parameterTypes)
        {
            var parameters = new List <ParameterExpression>();

            var parameterDictionary = scope.CreateParameter(typeof(IDictionary));

            parameters.Add(parameterDictionary);

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy <Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    Expression innerExpression = Expression.Property(parameterDictionary, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                                      Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                                      Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return(innerParameter);
                }));
            }

            return(parameters);
        }
        /// <summary>Resolve enumerable parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List<ParameterExpression> ResolveParameterEnumerable(ExpressionScope scope, IDictionary<string, Type> parameterTypes)
        {
            var parameters = new List<ParameterExpression>();

            var parameterEnumerable = scope.CreateParameter(typeof (IEnumerable));
            parameters.Add(parameterEnumerable);

            var dictParameter = scope.CreateVariable(typeof (Dictionary<string, object>));
            var methodConvert = typeof (EvalCompiler).GetMethod("ResolveToParameterDictionary", BindingFlags.NonPublic | BindingFlags.Static);
            var expressionConvert = Expression.Call(methodConvert, new Expression[] {parameterEnumerable});
            var expressionAssign = Expression.Assign(dictParameter, expressionConvert);
            scope.Expressions.Add(expressionAssign);

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new LazySingleThread<Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    Expression innerExpression = Expression.Property(dictParameter, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                        Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                        Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return innerParameter;
                }));
            }

            return parameters;
        }
        /// <summary>Resolve enumerable parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterEnumerable(ExpressionScope scope, IDictionary <string, Type> parameterTypes)
        {
            var parameters = new List <ParameterExpression>();

            var parameterEnumerable = scope.CreateParameter(typeof(IEnumerable));

            parameters.Add(parameterEnumerable);

            var dictParameter     = scope.CreateVariable(typeof(Dictionary <string, object>));
            var methodConvert     = typeof(EvalCompiler).GetMethod("ResolveToParameterDictionary", BindingFlags.NonPublic | BindingFlags.Static);
            var expressionConvert = Expression.Call(methodConvert, new Expression[] { parameterEnumerable });
            var expressionAssign  = Expression.Assign(dictParameter, expressionConvert);

            scope.Expressions.Add(expressionAssign);

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy <Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    Expression innerExpression = Expression.Property(dictParameter, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                                      Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                                      Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return(innerParameter);
                }));
            }

            return(parameters);
        }
        /// <summary>Resolve untyped parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List<ParameterExpression> ResolveParameterUntyped(ExpressionScope scope, IDictionary<string, Type> parameterTypes, bool forceFirstParameterProperty = false)
        {
            var parameters = new List<ParameterExpression>();

            foreach (var parameter in parameterTypes)
            {
                var parameterExpression = scope.CreateParameter(typeof (object));
                parameters.Add(parameterExpression);

                scope.CreateLazyVariable(parameter.Key, new Lazy<Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    var innerExpression = parameterExpression.Type != parameter.Value ?
                        Expression.Assign(innerParameter, Expression.Convert(parameterExpression, parameter.Value)) :
                        Expression.Assign(innerParameter, parameterExpression);

                    scope.Expressions.Insert(0, innerExpression);

                    return innerParameter;
                }));
            }

            if (parameterTypes.Count == 1 || (parameterTypes.Count > 0 && forceFirstParameterProperty))
            {
                var keyValue = parameterTypes.First();
                if (Type.GetTypeCode(keyValue.Value) == TypeCode.Object)
                {
                    ResolzeLazyMember(scope, parameterTypes, keyValue.Key, keyValue.Value);
                }
            }

            return parameters;
        }
Пример #6
0
        /// <summary>Resolve untyped parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterUntyped(ExpressionScope scope, IDictionary <string, Type> parameterTypes)
        {
            var parameters = new List <ParameterExpression>();

            foreach (var parameter in parameterTypes)
            {
                var parameterExpression = scope.CreateParameter(typeof(object));
                parameters.Add(parameterExpression);

                scope.CreateLazyVariable(parameter.Key, new LazySingleThread <Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    var innerExpression = parameterExpression.Type != parameter.Value ?
                                          Expression.Assign(innerParameter, Expression.Convert(parameterExpression, parameter.Value)) :
                                          Expression.Assign(innerParameter, parameterExpression);

                    scope.Expressions.Insert(0, innerExpression);

                    return(innerParameter);
                }));
            }

            if (parameterTypes.Count == 1)
            {
                var keyValue = parameterTypes.First();
                if (Type.GetTypeCode(keyValue.Value) == TypeCode.Object)
                {
                    ResolzeLazyMember(scope, parameterTypes, keyValue.Key, keyValue.Value);
                }
            }

            return(parameters);
        }
        /// <summary>Resolve single object parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List<ParameterExpression> ResolveParameterSingleObject(ExpressionScope scope, IDictionary<string, Type> parameterTypes)
        {
            var parameters = new List<ParameterExpression>();

            var parameterExpression = scope.CreateParameter(typeof (object));
            parameters.Add(parameterExpression);

            var parameterObjectType = parameterTypes["{0}"];
            var parameterObject = scope.CreateVariable(parameterObjectType, "{0}");
            scope.Expressions.Add(Expression.Assign(parameterObject, Expression.Convert(parameterExpression, parameterObjectType)));

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy<Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    var property = parameterObjectType.GetProperty(parameter.Key);

                    Expression innerExpression = property != null ?
                        Expression.Property(parameterObject, property) :
                        Expression.Field(parameterObject, parameterObjectType.GetField(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                        Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                        Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return innerParameter;
                }));
            }

            return parameters;
        }
Пример #8
0
        /// <summary>Resolve typed parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterTyped(ExpressionScope scope, IDictionary <string, Type> parameterTypes)
        {
            var parameters = new List <ParameterExpression>();

            foreach (var parameter in parameterTypes)
            {
                parameters.Add(scope.CreateParameter(parameter.Value, parameter.Key));
            }

            if (parameterTypes.Count == 1)
            {
                var keyValue = parameterTypes.First();
                if (Type.GetTypeCode(keyValue.Value) == TypeCode.Object)
                {
                    ResolzeLazyMember(scope, parameterTypes, keyValue.Key, keyValue.Value);
                }
            }

            return(parameters);
        }
        /// <summary>Resolve typed parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List<ParameterExpression> ResolveParameterTyped(ExpressionScope scope, IDictionary<string, Type> parameterTypes)
        {
            var parameters = new List<ParameterExpression>();

            foreach (var parameter in parameterTypes)
            {
                parameters.Add(scope.CreateParameter(parameter.Value, parameter.Key));
            }

            if (parameterTypes.Count == 1)
            {
                var keyValue = parameterTypes.First();
                if (Type.GetTypeCode(keyValue.Value) == TypeCode.Object)
                {
                    ResolzeLazyMember(scope, parameterTypes, keyValue.Key, keyValue.Value);
                }
            }

            return parameters;
        }
Пример #10
0
        /// <summary>Resolve single object parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterSingleObject(ExpressionScope scope, IDictionary <string, Type> parameterTypes)
        {
            var parameters = new List <ParameterExpression>();

            var parameterExpression = scope.CreateParameter(typeof(object));

            parameters.Add(parameterExpression);

            var parameterObjectType = parameterTypes["{0}"];
            var parameterObject     = scope.CreateVariable(parameterObjectType, "{0}");

            scope.Expressions.Add(Expression.Assign(parameterObject, Expression.Convert(parameterExpression, parameterObjectType)));

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy <Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    var property = parameterObjectType.GetProperty(parameter.Key);

                    Expression innerExpression = property != null ?
                                                 Expression.Property(parameterObject, property) :
                                                 Expression.Field(parameterObject, parameterObjectType.GetField(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                                      Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                                      Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return(innerParameter);
                }));
            }

            return(parameters);
        }
        /// <summary>Resolve single dictionary parameters used for the code or expression.</summary>
        /// <param name="scope">The expression scope for the code or expression to compile.</param>
        /// <param name="parameterTypes">The dictionary of parameter (name / type) used in the code or expression to compile.</param>
        /// <returns>A ParameterExpression list used in code or expression to compile.</returns>
        private static List <ParameterExpression> ResolveParameterSingleDictionary(ExpressionScope scope, IDictionary <string, Type> parameterTypes, bool forceFirstParameterProperty = false)
        {
            var parameters = new List <ParameterExpression>();

            var parameterDictionary = scope.CreateParameter(typeof(IDictionary), "{0}");

            parameters.Add(parameterDictionary);

            foreach (var parameter in parameterTypes)
            {
                scope.CreateLazyVariable(parameter.Key, new Lazy <Expression>(() =>
                {
                    var innerParameter = scope.CreateVariable(parameter.Value, parameter.Key);

                    Expression innerExpression = Expression.Property(parameterDictionary, DictionaryItemPropertyInfo, Expression.Constant(parameter.Key));

                    innerExpression = innerExpression.Type != parameter.Value ?
                                      Expression.Assign(innerParameter, Expression.Convert(innerExpression, parameter.Value)) :
                                      Expression.Assign(innerParameter, innerExpression);

                    scope.Expressions.Add(Expression.Assign(innerParameter, innerExpression));

                    return(innerParameter);
                }));
            }

            if (parameterTypes.Count > 0 && forceFirstParameterProperty)
            {
                var keyValue = parameterTypes.First();
                if (Type.GetTypeCode(keyValue.Value) == TypeCode.Object)
                {
                    ResolzeLazyMember(scope, parameterTypes, keyValue.Key, keyValue.Value);
                }
            }

            return(parameters);
        }