Example #1
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="type1">The first type used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Func <object, object> Compile(string code, Type type1)
 {
     return(EvalCompiler.Compile <Func <object, object> >(this, code, new Dictionary <string, Type>
     {
         { "{0}", type1 }
     }, typeof(object), EvalCompilerParameterKind.Untyped));
 }
Example #2
0
        /// <summary>Compile the code or expression and return a TDelegate of type Func or Action to execute.</summary>
        /// <typeparam name="TDelegate">Type of the delegate (Func or Action) to use to compile the code or expression.</typeparam>
        /// <param name="code">The code or expression to compile.</param>
        /// <param name="parameterNames">Parameter names used to compile the code or expressions.</param>
        /// <returns>A TDelegate of type Func or Action that represents the compiled code or expression.</returns>
        public TDelegate Compile <TDelegate>(string code, params string[] parameterNames)
        {
            var parameterTypes = new Dictionary <string, Type>();

            var tDelegate = typeof(TDelegate);
            var arguments = tDelegate.GetGenericArguments();
            var isAction  = tDelegate.FullName.StartsWith("System.Action");

            var tReturn = isAction ? null : arguments.Last();
            var lastArgumentPosition = isAction ? arguments.Length : arguments.Length - 1;

            for (var i = 0; i < lastArgumentPosition; i++)
            {
                if (parameterNames != null && i <= parameterNames.Length)
                {
                    parameterTypes.Add(parameterNames[i], arguments[i]);
                }
                else
                {
                    parameterTypes.Add(string.Concat("{", i, "}"), arguments[i]);
                }
            }

            return(EvalCompiler.Compile <TDelegate>(this, code, parameterTypes, tReturn, EvalCompilerParameterKind.Typed));
        }
Example #3
0
        /// <summary>Compile and evaluate the code or expression and return the result of type TResult.</summary>
        /// <typeparam name="TResult">Type of the result of the evaluted code or expression.</typeparam>
        /// <param name="code">The code or expression to evaluate.</param>
        /// <param name="parameters">The parameter values used to evaluates the code or expression.</param>
        /// <returns>The evaluated result of type TResult or null that represents the evaluted code or expression.</returns>
        public TResult Execute <TResult>(string code, object parameters)
        {
            var parameterTypes = new Dictionary <string, Type> {
                { "{0}", parameters.GetType() }
            };

            if (parameters is IDictionary)
            {
                foreach (DictionaryEntry entry in (IDictionary)parameters)
                {
                    parameterTypes.Add(entry.Key.ToString(), entry.Value.GetType());
                }

                return(EvalCompiler.Compile <Func <IDictionary, TResult> >(this, code, parameterTypes, typeof(TResult), EvalCompilerParameterKind.SingleDictionary)((IDictionary)parameters));
            }
            else if (parameters is ExpandoObject)
            {
                var dictValues = new Dictionary <string, object>();
                dictValues.Add("{0}", parameters);

                foreach (var entry in (IDictionary <string, object>)parameters)
                {
                    parameterTypes.Add(entry.Key, entry.Value.GetType());
                    dictValues.Add(entry.Key, entry.Value);
                }

                return(EvalCompiler.Compile <Func <Dictionary <string, object>, TResult> >(this, code, parameterTypes, typeof(TResult), EvalCompilerParameterKind.SingleDictionary)(dictValues));
            }

            return(EvalCompiler.Compile <Func <object, TResult> >(this, code, parameterTypes, typeof(TResult), EvalCompilerParameterKind.Untyped)(parameters));
        }
Example #4
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="type1">The first type used to compile the code or expression.</param>
 /// <param name="type2">The second type used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Task <Func <object, object, object> > CompileAsync(string code, Type type1, Type type2)
 {
     return(EvalCompiler.CompileAsync <Func <object, object, object> >(this, code, new Dictionary <string, Type>
     {
         { "{0}", type1 },
         { "{1}", type2 }
     }, typeof(object), EvalCompilerParameterKind.Untyped));
 }
Example #5
0
        /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
        /// <param name="code">The code or expression to compile.</param>
        /// <param name="parameterTypes">Parameter types used to compile the code or expression.</param>
        /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
        public Func <IEnumerable, object> Compile(string code, params Type[] parameterTypes)
        {
            var dict = new Dictionary <string, Type>();

            for (var i = 0; i < parameterTypes.Length; i++)
            {
                dict.Add(string.Concat("{", i, "}"), parameterTypes[i]);
            }

            return(EvalCompiler.Compile <Func <IEnumerable, object> >(this, code, dict, typeof(object), EvalCompilerParameterKind.Enumerable));
        }
Example #6
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="type1">The first type used to compile the code or expression.</param>
 /// <param name="type2">The second type used to compile the code or expression.</param>
 /// <param name="type3">The third type used to compile the code or expression.</param>
 /// <param name="type4">The fourth type used to compile the code or expression.</param>
 /// <param name="type5">The fifth type used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Func <object, object, object, object, object, object> Compile(string code, Type type1, Type type2, Type type3, Type type4, Type type5)
 {
     return(EvalCompiler.Compile <Func <object, object, object, object, object, object> >(this, code, new Dictionary <string, Type>
     {
         { "{0}", type1 },
         { "{1}", type2 },
         { "{2}", type3 },
         { "{3}", type4 },
         { "{4}", type5 }
     }, typeof(object), EvalCompilerParameterKind.Untyped));
 }
Example #7
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="type1">The first type used to compile the code or expression.</param>
 /// <param name="type2">The second type used to compile the code or expression.</param>
 /// <param name="type3">The third type used to compile the code or expression.</param>
 /// <param name="type4">The fourth type used to compile the code or expression.</param>
 /// <param name="type5">The fifth type used to compile the code or expression.</param>
 /// <param name="type6">The sixth type used to compile the code or expression.</param>
 /// <param name="type7">The seventh type used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Task <Func <object, object, object, object, object, object, object, object> > CompileAsync(string code, Type type1, Type type2, Type type3, Type type4, Type type5, Type type6, Type type7)
 {
     return(EvalCompiler.CompileAsync <Func <object, object, object, object, object, object, object, object> >(this, code, new Dictionary <string, Type>
     {
         { "{0}", type1 },
         { "{1}", type2 },
         { "{2}", type3 },
         { "{3}", type4 },
         { "{4}", type5 },
         { "{5}", type6 },
         { "{6}", type7 }
     }, typeof(object), EvalCompilerParameterKind.Untyped));
 }
Example #8
0
        /// <summary>Compile and evaluate the code or expression and return the result of type TResult.</summary>
        /// <typeparam name="TResult">Type of the result of the evaluted code or expression.</typeparam>
        /// <param name="code">The code or expression to evaluate.</param>
        /// <param name="parameters">The parameter values used to evaluates the code or expression.</param>
        /// <returns>The evaluated result of type TResult or null that represents the evaluted code or expression.</returns>
        public TResult Execute <TResult>(string code, params object[] parameters)
        {
            var dictValues = new Dictionary <string, object>();
            var dictTypes  = new Dictionary <string, Type>();

            for (var i = 0; i < parameters.Length; i++)
            {
                var key = string.Concat("{", i, "}");
                dictValues.Add(key, parameters[i]);
                dictTypes.Add(key, parameters[i].GetType());
            }

            return(EvalCompiler.Compile <Func <IDictionary, TResult> >(this, code, dictTypes, typeof(TResult), EvalCompilerParameterKind.Dictionary)(dictValues));
        }
Example #9
0
        public static object Execute(string code, Expression exp = null, params object[] parameters)
        {
            if (exp != null)
            {
                var expression = exp.ToString();
                expression = expression.Remove(expression.IndexOf('"'), 1);
                expression = expression.Remove(expression.LastIndexOf('"'), 1);
                code       = code.Replace("{expression}", expression);
            }

            var dictValues = new Dictionary <string, object>();
            var dictTypes  = new Dictionary <string, Type>();

            for (var i = 0; i < parameters.Length; i++)
            {
                if (i == 0)
                {
                    dictValues.Add("{" + i + parameters.Length + "}", parameters[i]);

                    if (parameters[i] != null)
                    {
                        dictTypes.Add("{" + i + parameters.Length + "}", parameters[i].GetType());
                    }
                    else
                    {
                        dictTypes.Add("{" + i + parameters.Length + "}", typeof(object));
                    }
                }
                else
                {
                    dictValues.Add("{" + i + "}", parameters[i]);

                    if (parameters[i] != null)
                    {
                        dictTypes.Add("{" + i + "}", parameters[i].GetType());
                    }
                    else
                    {
                        dictTypes.Add("{" + i + "}", typeof(object));
                    }
                }
            }

            var result = EvalCompiler.Compile <Func <IDictionary, object> >(EvalManager.DefaultContext, code, dictTypes, typeof(object), EvalCompilerParameterKind.SingleDictionary, true)(dictValues);

            return(result);
        }
        /// <summary>Compile and evaluate the code or expression and return the result of type TResult.</summary>
        /// <typeparam name="TResult">Type of the result of the evaluted code or expression.</typeparam>
        /// <param name="code">The code or expression to evaluate.</param>
        /// <param name="parameters">The parameter values used to evaluates the code or expression.</param>
        /// <returns>The evaluated result of type TResult or null that represents the evaluted code or expression.</returns>
        public Task <TResult> ExecuteAsync <TResult>(string code, object parameters)
        {
            var parameterTypes = new Dictionary <string, Type> {
                { "{0}", parameters.GetType() }
            };

            if (parameters is IDictionary)
            {
                foreach (DictionaryEntry entry in (IDictionary)parameters)
                {
                    parameterTypes.Add(entry.Key.ToString(), entry.Value.GetType());
                }

                return(Task.Run(() => EvalCompiler.Compile <Func <IDictionary, TResult> >(this, code, parameterTypes, typeof(TResult), EvalCompilerParameterKind.SingleDictionary)((IDictionary)parameters)));
            }

            return(Task.Run(() => EvalCompiler.Compile <Func <object, TResult> >(this, code, parameterTypes, typeof(TResult), EvalCompilerParameterKind.Untyped)(parameters)));
        }
Example #11
0
 /// <summary>Compile and evaluate the code or expression and return the result of type TResult.</summary>
 /// <typeparam name="TResult">Type of the result of the evaluted code or expression.</typeparam>
 /// <param name="code">The code or expression to evaluate.</param>
 /// <returns>The evaluated result of type TResult or null that represents the evaluted code or expression.</returns>
 public TResult Execute <TResult>(string code)
 {
     return(EvalCompiler.Compile <Func <TResult> >(this, code, null, typeof(TResult), EvalCompilerParameterKind.None)());
 }
Example #12
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="parameterTypes">Parameter types used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Func <IDictionary, object> Compile(string code, IDictionary <string, Type> parameterTypes)
 {
     return(EvalCompiler.Compile <Func <IDictionary, object> >(this, code, parameterTypes, typeof(object), EvalCompilerParameterKind.Dictionary));
 }
Example #13
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public Func <object> Compile(string code)
 {
     return(EvalCompiler.Compile <Func <object> >(this, code, null, typeof(object), EvalCompilerParameterKind.None));
 }
 /// <summary>Compile and evaluate the code or expression and return the result of type TResult.</summary>
 /// <typeparam name="TResult">Type of the result of the evaluted code or expression.</typeparam>
 /// <param name="code">The code or expression to evaluate.</param>
 /// <returns>The evaluated result of type TResult or null that represents the evaluted code or expression.</returns>
 public async Task <TResult> ExecuteAsync <TResult>(string code)
 {
     return(EvalCompiler.CompileAsync <Func <TResult> >(this, code, null, typeof(TResult), EvalCompilerParameterKind.None).Result());
 }
Example #15
0
 /// <summary>Compile the code or expression and return a delegate of type Func to execute.</summary>
 /// <param name="code">The code or expression to compile.</param>
 /// <param name="parameterTypes">Parameter types used to compile the code or expression.</param>
 /// <returns>A delegate of type Func that represents the compiled code or expression.</returns>
 public EvalDelegate CompileSQLNET(string code, IDictionary <string, Type> parameterTypes)
 {
     return(EvalCompiler.CompileSQLNET(this, code, parameterTypes, typeof(object)));
 }