public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables) { S4JTokenFunction function = token as S4JTokenFunction; StringBuilder code = new StringBuilder(); CSharpEvaluatorGlobals globals = new CSharpEvaluatorGlobals(); IDictionary <string, object> globaVariables = globals.Globals as IDictionary <string, object>; // var globalObject = new Dictionary<string, object>(); foreach (KeyValuePair <string, object> keyAndVal in variables) { globaVariables[keyAndVal.Key] = keyAndVal.Value; code.Append("var ").Append(keyAndVal.Key).Append(" = ").Append("Globals.").Append(keyAndVal.Key).Append(";"); } dynamic dbProxy = new ExpandoObject(); foreach (var source in Executor.Sources) { (dbProxy as IDictionary <string, object>)[source.Key] = new DbApi(source.Value); } globaVariables["db"] = dbProxy; code.Append("var ").Append("db").Append(" = ").Append("Globals.").Append("db").Append(";"); code.Append(function.ToJsonWithoutGate()); var refs = new List <MetadataReference> { MetadataReference.CreateFromFile(typeof(System.Linq.Enumerable).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(System.Runtime.CompilerServices.DynamicAttribute).GetTypeInfo().Assembly.Location) }; var imports = ScriptOptions.Default. WithImports( "System", "System.Text", "System.Linq", "System.Collections", "System.Collections.Generic"). WithReferences(refs); object result = await CSharpScript.EvaluateAsync( code.ToString(), imports, globals); return(result); }
public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables) { S4JTokenFunction function = token as S4JTokenFunction; StringBuilder code = new StringBuilder(); DynLanEvaluatorGlobals globals = new DynLanEvaluatorGlobals(); IDictionary <string, object> globaVariables = globals.Globals as IDictionary <string, object>; // var globalObject = new Dictionary<string, object>(); foreach (KeyValuePair <string, object> keyAndVal in variables) { globaVariables[keyAndVal.Key /*.ToUpper()*/] = keyAndVal.Value; /*if (keyAndVal.Value == null) * { * code.Append($"object {keyAndVal.Key} = {keyAndVal.Value.SerializeJson()};\n"); * } * else * { * code.Append($"var {keyAndVal.Key} = {keyAndVal.Value.SerializeJson()};\n"); * }*/ } Dictionary <String, Object> dbProxy = new Dictionary <String, Object>(); foreach (var source in Executor.Sources) { dbProxy[source.Key] = new DbApi(source.Value); } globaVariables["db"] = dbProxy; code.Append(function.ToJsonWithoutGate()); // string finalCode = MyStringHelper.AddReturnStatement(code.ToString()); Object result = new Compiler(). Compile(code.ToString()). Eval(globaVariables); return(result); }
public async Task <Object> Evaluate(S4JExecutor Executor, S4JToken token, IDictionary <String, object> variables) { S4JTokenFunction functionToken = token as S4JTokenFunction; S4JStateFunction functionState = token.State as S4JStateFunction; MyQuery query = new MyQuery(); foreach (KeyValuePair <string, object> keyAndVal in variables) { BuildScriptForVariable(query, keyAndVal.Key, keyAndVal.Value); } query.Append(functionToken.ToJsonWithoutGate()); String connectionString = Executor.Sources.Get(functionState.FunctionName); using (SqlConnection con = new SqlConnection(connectionString)) { var result = con.SelectItems(query.ToString()); return(result); } }