private static object ExecuteScript(string script, ScriptExecutionOptions options) { object result = null; CompilerParameters cpar = new CompilerParameters(); cpar.GenerateInMemory = true; cpar.ReferencedAssemblies.Add("mscorlib.dll"); cpar.ReferencedAssemblies.Add("System.dll"); cpar.ReferencedAssemblies.Add("System.Core.dll"); cpar.ReferencedAssemblies.Add("System.Data.dll"); cpar.ReferencedAssemblies.Add("System.Xml.dll"); cpar.ReferencedAssemblies.Add(typeof(CSharpScriptingProvider).Assembly.Location); string code = Properties.Resources.ScriptProviderCSharpTemplate; code = code.Replace("{Source}", script); CSharpCodeProvider csp = new CSharpCodeProvider(); CompilerResults res = csp.CompileAssemblyFromSource(cpar, code); if (!res.Errors.HasErrors) { MethodInfo func = res.CompiledAssembly.ExportedTypes.First().GetMethods().First(); result = func.Invoke(null, new object[] { options }); } return result; }
/// <summary> /// Searches for a <see cref="IScriptingProvider"/> that can execute the script type as stated in <see cref="ScriptTypeKey"/> and invokes the script. /// </summary> /// <param name="context"></param> protected override void RetrieveData(IGenerationContext context) { IScriptingProvider[] matchingProviders = context.Engine.Extensions.Get <IScriptingProvider>().Where(_ => _.CanExecute(this.ScriptTypeKey)).ToArray(); /* We need at least one matching provider. If there is more than one matching provider, take the first one and maybe omit warning. */ if (matchingProviders.Length > 0) { IScriptingProvider prov = matchingProviders.First(); ScriptExecutionOptions options = new ScriptExecutionOptions(); options.AssociatedDataProvider = this; options.DesiredReturnValueType = typeof(DataSet); try { object ret = prov.Execute(this.ScriptText, options); DataSet ds = ret as DataSet; if (ds != null) { this.CurrentData = ds; } } catch (Exception) { throw; } } }
internal ScriptContext(ScriptExecutionOptions options) { this.@params = (options.Parameters != null) ? options.Parameters.ToArray() : new object[0]; this.ret = new object(); if (options.DesiredReturnValueType != null) { this.ret = Activator.CreateInstance(options.DesiredReturnValueType); } }
object IScriptingProvider.Execute(string script, ScriptExecutionOptions options) { ScriptContext context = new ScriptContext(options); Engine eng = new Engine(_ => _.Strict()); eng.SetValue("context", context); object v = eng.Execute(script); if (context.ret != null) { return context.ret; } return v; }
object IScriptingProvider.Execute(string script, ScriptExecutionOptions options) { ScriptContext context = new ScriptContext(options); Engine eng = new Engine(_ => _.Strict()); eng.SetValue("context", context); object v = eng.Execute(script); if (context.ret != null) { return(context.ret); } return(v); }
object IScriptingProvider.Execute(string script, ScriptExecutionOptions options) { return ExecuteScript(script, options); }
/// <summary> /// Searches for a <see cref="IScriptingProvider"/> that can execute the script type as stated in <see cref="ScriptTypeKey"/> and invokes the script. /// </summary> /// <param name="context"></param> protected override void RetrieveData(IGenerationContext context) { IScriptingProvider[] matchingProviders = context.Engine.Extensions.Get<IScriptingProvider>().Where(_ => _.CanExecute(this.ScriptTypeKey)).ToArray(); /* We need at least one matching provider. If there is more than one matching provider, take the first one and maybe omit warning. */ if (matchingProviders.Length > 0) { IScriptingProvider prov = matchingProviders.First(); ScriptExecutionOptions options = new ScriptExecutionOptions(); options.AssociatedDataProvider = this; options.DesiredReturnValueType = typeof(DataSet); try { object ret = prov.Execute(this.ScriptText, options); DataSet ds = ret as DataSet; if (ds != null) { this.CurrentData = ds; } } catch (Exception) { throw; } } }