/// <summary> /// Runs and compiles a linq-query /// </summary> /// <param name="configuration">the configuration that is used for compiling the provided linq-query</param> /// <param name="target">the target object on which to run the query</param> /// <param name="nameOfTarget">the name of the parameter holding the target-object</param> /// <param name="expression">the expression to run on the target object</param> /// <param name="arguments">arguments for the query. each property of the given object will lead to a parameter of the resulting method</param> /// <returns>the result of the compiled and executed method</returns> public static object RunLinqQuery(string configuration, object target, string nameOfTarget, string expression, IDictionary <string, object> arguments) { var cfg = configurations.GetOrAdd(configuration, new NativeConfiguration()); if (cfg.AutoReferences) { ApplyAutoRef(cfg, arguments.Values, false); ApplyAutoRef(cfg, new[] { target }, true); } string roslynHash = GetFlatString(expression); var roslynScript = roslynScripts.GetOrAdd(roslynHash, new Lazy <ScriptRunner <object> >(() => { ScriptOptions scriptoptions; lock (cfg) { scriptoptions = ScriptOptions.Default .WithImports(cfg.Usings.Union(new[] { "System", "System.Linq", "System.Collections.Generic" })) .WithReferences( new[] { typeof(FileStyleUriParser).Assembly, typeof(Action).Assembly, NamedAssemblyResolve.LoadAssembly("System.Linq"), NamedAssemblyResolve.LoadAssembly("Microsoft.CSharp") }.Union(from t in cfg.References select NamedAssemblyResolve.LoadAssembly(t)).ToArray()) .WithOptimizationLevel(OptimizationLevel.Release); } //var retVal = CSharpScript.Create(expression, scriptoptions, typeof(NativeScriptObjectHelper), Loader); var retVal = CSharpScript.Create(expression, scriptoptions, typeof(NativeScriptObjectHelper)); retVal.Compile(); return(retVal.CreateDelegate()); })); var dic = new ExpandoObject(); var idic = dic as IDictionary <string, object>; idic[nameOfTarget] = target; foreach (var argument in arguments) { idic[argument.Key] = argument.Value; } return(AsyncHelpers.RunSync(() => roslynScript.Value(new NativeScriptObjectHelper { Global = idic }))); }
/// <summary> /// Creates a Command Executor object that is capable for executing the requested command /// </summary> /// <param name="command">the Query to execute on the offline tables</param> /// <returns>a procy object that is capable for executing the requested query</returns> public static Script <object> CreateCommand(string command) { var roslynHash = GetHashSha256(command); var roslynScript = roslynScripts.GetOrAdd(roslynHash, new Lazy <Script <object> >(() => { var scriptoptions = ScriptOptions.Default.WithImports(dynamicUsings.Union(new[] { "System", "System.Linq", "System.Collections.Generic", "ITVComponents", "ITVComponents.DataAccess", "ITVComponents.DataAccess.Extensions", "ITVComponents.DataAccess.Linq" })).WithReferences(new[] { typeof(FileStyleUriParser).Assembly, typeof(Action).Assembly }.Union(from t in Extensions.Union(dynamicExtensions).Distinct() select NamedAssemblyResolve.LoadAssembly(t))); return(CSharpScript.Create(command, scriptoptions, typeof(NativeScriptObjectHelper))); })); return(roslynScript.Value); }