private void cmdExecute_Click(object sender, System.EventArgs e) { wwScripting loScript = new wwScripting(this.txtLanguage.Text); loScript.lSaveSourceCode = true; // loScript.CreateAppDomain("WestWind"); // force into AppDomain loScript.AddAssembly("system.windows.forms.dll", "System.Windows.Forms"); //loScript.AddAssembly("system.web.dll","System.Web"); //loScript.AddNamespace("System.Net"); string lcCode = this.txtCode.Text; int x = 100; string lcResult = (string)loScript.ExecuteCode(lcCode, "rick strahl", (int)x, (decimal)10); //*** Execute full method or mutliple methods on the same object // string lcResult = (string) loScript.ExecuteMethod(lcCode,"Test","rick strahl",(int) x); // lcResult = (string) loScript.CallMethod(loScript.oObjRef,"Test2","rick strahl",(int) x); this.txtGeneratedCode.Text = loScript.cSourceCode; if (loScript.bError) { MessageBox.Show(loScript.cErrorMsg + "\r\n\r\n" + loScript.cSourceCode, "Compile ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show(lcResult, "Code Result"); } loScript.Dispose(); }
private void _buildAndExecuteCode(object sender, EventArgs e) { wwScripting wwScript = null; try { //log.Info("User Context Code is running under: " + System.Security.Principal.WindowsIdentity.GetCurrent().Name); wwScript = new wwScripting(Language); wwScript.lSaveSourceCode = false; //TODO: figure out how to add these from the GAC or by relative path //It looks like we have to use a file path: http://www.databaseforum.info/25/860284.aspx //however GAC does have a file path behind it (i.e. C:\Windows\assembly\GAC_MSIL\wwScripting\1.0.4486.26865__e7bb1946e9e55389\wwScripting.dll) wwScript.cSupportAssemblyPath = "C:\\Installs\\"; Environment.CurrentDirectory = System.IO.Path.GetTempPath(); //TODO: fix wwScript.dll to get AppDomains working: currently fails on 371 of the //CreateInstance method because the RemoteLoader dll cant be found, will need to //change reference to use full assembly name, ie. RemoteLoader, Version=1.0.4406.26870, Culture=neutral, PublicKeyToken=739e02ea3855a61b //to pull it from the GAC. I have already signed them for a strong name and put them there wwScript.CreateAppDomain("Insight.FIM.Workflow.CustomCode"); // force into AppDomain //loScript.AddAssembly("system.windows.forms.dll", "System.Windows.Forms"); //loScript.AddAssembly("system.web.dll","System.Web"); //wwScript.AddNamespace("System.Net"); foreach (string ns in Includes.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { //log.Info("Adding namespace: " + ns); if (!string.IsNullOrEmpty(ns.Trim())) { wwScript.AddNamespace(ns.Trim()); } } foreach (string a in Assemblies.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { //log.Info("Adding assembly: " + a); if (!string.IsNullOrEmpty(a.Trim())) { wwScript.AddAssembly(a.Trim()); } } //user is passing in a return type, do we need to perform //some conversion here - No, we dont. we just need to pass the //type on to the ChangeAttributeCodeActivity //what if we want to return a multi-value/array? CodeReturnValue = (string)wwScript.ExecuteCode(Code, paramArrayResolved); if (wwScript.bError) { throw new Exception("Error from wwScript - " + wwScript.cErrorMsg); } log.Debug("Custom code execution complete. Return value: " + CodeReturnValue); } catch (Exception ex) { log.Error("An exception occured while executing the custom code. Details: " + ex.ToString()); if (ex.InnerException != null) { log.Error("InnerException: " + ex.InnerException.ToString()); } throw; } finally { if (wwScript != null) { wwScript.Dispose(); } } }
/// <summary> /// Evaluates the embedded script parsing out {{ C# Expression }} /// blocks and evaluating the expressions and embedding the string /// output into the result string. /// /// /// </summary> /// <param name="code">The code to execute /// <param name="model">Optional model data accessible in Expressions as `Model`</param> /// <returns></returns> public bool EvaluateScript(string code, object model = null) { ScriptInstance = CreatewwScripting(); if (CodeBlocks.ContainsKey(code)) { Debug.WriteLine("wwScripting Cached Code: \r\n" + code); ScriptInstance.ExecuteCodeFromAssembly(code, CodeBlocks[code], model); } else { var snippetLines = StringUtils.GetLines(code); var sb = new StringBuilder(); foreach (var line in snippetLines) { if (line.Trim().Contains("#r ")) { string assemblyName = line.Replace("#r ", "").Trim(); if (assemblyName.Contains("\\") || assemblyName.Contains("/")) { ErrorMessage = "Assemblies loaded from external folders are not allowed: " + assemblyName + "\r\n\r\n" + "Referenced assemblies can only be loaded out of the Markdown Monster startup folder."; return(false); } var fullAssemblyName = FileUtils.GetPhysicalPath(assemblyName); if (File.Exists(fullAssemblyName)) { assemblyName = fullAssemblyName; } // Add to Engine since host is already instantiated ScriptInstance.AddAssembly(assemblyName); continue; } if (line.Trim().Contains("using ")) { string ns = line.Replace("using ", "").Replace(";", "").Trim(); if (!ScriptInstance.Namespaces.Contains("using " + ns + ";")) { ScriptInstance.AddNamespace(ns); } continue; } sb.AppendLine(line); } string oldPath = Environment.CurrentDirectory; code = sb.ToString(); code = "dynamic Model = Parameters[0];\r\n" + code + "\r\n" + "return null;"; ScriptInstance.ExecuteCode(code, model); // cache the generated assembly for reuse on subsequent runs if (ScriptInstance.Assembly != null) { CodeBlocks[code] = ScriptInstance.Assembly; } Directory.SetCurrentDirectory(oldPath); } if (ScriptInstance.Error) { ErrorMessage = ScriptInstance.ErrorMessage; } return(!ScriptInstance.Error); }
/// <summary> /// Creates an instance of wwScripting for this parser /// with the appropriate assemblies and namespaces set /// </summary> /// <returns></returns> private static wwScripting CreatewwScripting() { var scripting = new wwScripting() { DefaultAssemblies = false, AssemblyNamespace = "MarkdownMonster.Commander.Scripting" }; scripting.AddAssembly("System.dll"); scripting.AddAssembly("System.Core.dll"); scripting.AddAssembly("Microsoft.CSharp.dll"); scripting.AddAssembly("System.Windows.Forms.dll"); scripting.AddAssembly("System.Data.dll"); scripting.AddAssembly("MarkdownMonster.exe"); scripting.AddAssembly("Westwind.Utilities.dll"); scripting.AddAssembly("System.Configuration.dll"); scripting.AddAssembly("WPF\\PresentationCore.dll"); scripting.AddAssembly("WPF\\PresentationUI.dll"); scripting.AddAssembly("WPF\\PresentationFramework.dll"); scripting.AddAssembly("WPF\\WindowsBase.dll"); scripting.AddAssembly("System.Xaml.dll"); scripting.AddNamespace("System"); scripting.AddNamespace("System.IO"); scripting.AddNamespace("System.Reflection"); scripting.AddNamespace("System.Text"); scripting.AddNamespace("System.Drawing"); scripting.AddNamespace("System.Diagnostics"); scripting.AddNamespace("System.Data"); scripting.AddNamespace("System.Data.SqlClient"); scripting.AddNamespace("System.Linq"); scripting.AddNamespace("MarkdownMonster"); scripting.AddNamespace("Westwind.Utilities"); scripting.SaveSourceCode = true; return(scripting); }