/// <summary> /// Reads the xmlfile containing the information of the script executes it /// and returns the assembly if the script is being /// excuted or null if no ecript executed or an error occures. /// </summary> /// <param name="scriptname">the full path in which the script's directory is located</param> /// <param name="gui">the gui of the application that will /// execute the script</param> /// <returns>the assembly which is the result of the executed script</returns> public object ReadScript(string scriptname, WareForm gui) { try { int i = 0; object ap = null; DataSet set = new DataSet(); if ((scriptname != null) && (gui != null)) { string code, clas, func, names, tfilename; tfilename = scriptname + scriptext; string[] refs; ado.AttachDataBaseinDataSet(set, tfilename); refs = this.GetReferences(set); names = this.GetNameSace(set); code = this.GetCode(set); clas = this.GetClass(set); func = this.GetFunction(set); if ((definedConstants != null) && (definedValues != null) && (definedConstants.Length == definedValues.Length)) { for (i = 0; i < definedConstants.Length; i++) { code = replacer.findandreplaceDefinedConsts(code, definedConstants[i], definedValues[i]); code = replacer.findandreplaceSymbols(code); } } else if ((definedConstants == null) && (definedValues == null) && (definedConstants.Length == definedValues.Length)) { code = replacer.findandreplaceSymbols(code); } ScriptsSet = set; ScriptInfo scrinf = this.GetScriptInfo(set); scrinf.Filename = scriptname; ap = this.EvalWithParams(code.Split('\n'), scriptname, names, clas, func, refs, gui, scrinf); } return(ap); } catch (Exception e) { Program.Bugtracking(e); return(null); } }
public object EvalWithParams(string[] sCSCode, string scriptname, string NameSapce, string Clas, string func, string[] refs, WareForm winGUI, ScriptInfo scrinf) { try { if ((sCSCode != null) && (scriptname != null) && (NameSapce != null) && (Clas != null) && (func != null) && (refs != null) && (winGUI != null) && (scrinf != null) && (checkCompatibility(scrinf) == true)) { if ((scrinf.Language == "C#") || (scrinf.Language == "c#")) { ICodeCompiler icc = c.CreateCompiler(); cp.ReferencedAssemblies.Add("system.dll"); cp.ReferencedAssemblies.Add("system.xml.dll"); cp.ReferencedAssemblies.Add("system.data.dll"); // cp.ReferencedAssemblies.Add("system.windows.objects.dll"); cp.ReferencedAssemblies.Add("system.drawing.dll"); cp.ReferencedAssemblies.Add(Application.ExecutablePath); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\Hydrobase.dll"); //cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "HydrobaseSDK.dll"); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "WareForms.dll"); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "Scrabler.dll"); cp.ReferencedAssemblies.AddRange(refs); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; StringBuilder sb = new StringBuilder(""); /* sb.Append("using System;\n"); * sb.Append("using System.Xml;\n"); * sb.Append("using System.Data;\n"); * sb.Append("using System.Data.SqlClient;\n"); * // sb.Append("using System.Windows.objects;\n"); * sb.Append("using System.Drawing;\n"); * sb.Append("using Hydrobase;\n"); * sb.Append("using HydrobaseSDK;\n"); * * //sb.Append("using Frogy_Base_Hydrobase_Data_Enviroment;\n"); * sb.Append("using WareForms;\n"); * * /* sb.Append("namespace CSCodeEvaler{ \n"); * sb.Append("public class CSCodeEvaler{ \n"); * sb.Append("public object EvalCode(){\n"); * //sb.Append("return " + sCSCode + "; \n"); * sb.Append(sCSCode + "; \n"); */ /*sb.Append("} \n"); * sb.Append("} \n"); * sb.Append("}\n");*/ //string stat; //sb.Replace(func + "(WareForm gui", func + "( WareForm " + winGUI); // CompilerResults cr = icc.CompileAssemblyFromSource(cp, sb.ToString()); CompilerResults cr = icc.CompileAssemblyFromFileBatch(cp, sCSCode); if (cr.Errors.Count > 0) { // string tmp = sb.ToString().Replace("\n", Environment.NewLine); ErrorWindow errwin = new ErrorWindow(); // MessageBox.Show("ERROR in " + cr.Errors[0].Line + ": " + cr.Errors[0].ErrorText + cr.Errors[0].ToString() +"\n" + sb.ToString(), "Error evaluating cs code" , MessageBoxButtons.OK, MessageBoxIcon.Error); errwin.listErrors.Text = "ERROR in line " + cr.Errors[0].Line + ": " + cr.Errors[0].ErrorText; // errwin.txtCode.Text = cr. errwin.Show(); return(null); } System.Reflection.Assembly a = cr.CompiledAssembly; object o = a.CreateInstance(NameSapce + "." + Clas); AssemblyCol.Add(a, scrinf); Type t = o.GetType(); MethodInfo mi = t.GetMethod(func); WareForm[] oParams = new WareForm[1]; oParams[0] = winGUI; object s = mi.Invoke(o, oParams); return(s); } else if ((scrinf.Language == "VB") || (scrinf.Language == "vb")) { ICodeCompiler icc = vb.CreateCompiler(); // ErrorWindow errwin = new ErrorWindow(); cp.ReferencedAssemblies.Add("system.dll"); cp.ReferencedAssemblies.Add("system.xml.dll"); cp.ReferencedAssemblies.Add("system.data.dll"); // cp.ReferencedAssemblies.Add("system.windows.objects.dll"); cp.ReferencedAssemblies.Add("system.drawing.dll"); cp.ReferencedAssemblies.Add(Application.ExecutablePath); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\Hydrobase.dll"); // cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "HydrobaseSDK.dll"); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "WareForms.dll"); cp.ReferencedAssemblies.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", "") + "\\" + "Scrabler.dll"); cp.ReferencedAssemblies.AddRange(refs); cp.CompilerOptions = "/t:library"; cp.GenerateInMemory = true; /* StringBuilder sb = new StringBuilder(""); * sb.Append("Imports System\n"); * sb.Append("Imports System.Xml\n"); * sb.Append("Imports System.Data\n"); * sb.Append("Imports System.Data.SqlClient\n"); * * sb.Append("Imports System.Drawing\n"); * sb.Append("Imports Hydrobase\n"); * sb.Append("Imports HydrobaseSDK\n"); * * sb.Append("Imports WareForms\n"); * * * sb.Append(sCSCode + " \n");*/ CompilerResults cr = icc.CompileAssemblyFromFileBatch(cp, sCSCode); if (cr.Errors.Count > 0) { // MessageBox.Show("ERROR in " + cr.Errors[0].Line + ": " + cr.Errors[0].ErrorText + cr.Errors[0].ToString() +"\n" + sb.ToString(), "Error evaluating cs code" , MessageBoxButtons.OK, MessageBoxIcon.Error); // string tmp = sb.ToString().Replace("\n", Environment.NewLine); ErrorWindow errwin = new ErrorWindow(); errwin.listErrors.Text = "ERROR in line " + cr.Errors[0].Line + ": " + cr.Errors[0].ErrorText; //errwin.txtCode.Text = tmp; errwin.Show(); return(null); } System.Reflection.Assembly a = cr.CompiledAssembly; object o = a.CreateInstance(NameSapce + "." + Clas); AssemblyCol.Add(a, scrinf); Type t = o.GetType(); MethodInfo mi = t.GetMethod(func); WareForm[] oParams = new WareForm[1]; oParams[0] = winGUI; object s = mi.Invoke(o, oParams); // object s = mi.Invoke(o, null); return(s); } } return(null); } catch (Exception e) { Program.Bugtracking(e); return(null); } }
/*public abstract string[] GetReferences(DataSet set); * public abstract string GetNameSace(DataSet set); * public abstract string GetClass(DataSet set); * public abstract string GetFunction(DataSet set); * public abstract string GetCode(DataSet set); * public abstract ScriptInfo GetScriptInfo(DataSet set); * public abstract bool checkCompatibility(ScriptInfo scrinf); * public abstract object Eval(string sCSCode, string scriptname, string NameSapce, string Clas, string func, string[] refs, ScriptInfo scrinf); * public abstract object EvalWithParams(string sCSCode, string scriptname, string NameSapce, string Clas, string func, string[] refs, WareForm winGUI, ScriptInfo scrinf); * public abstract void SetReferences(DataSet set, string scriptsreffolder, string scriptfilename); * public abstract string GetProgrammLanguage(DataSet set); * */ public abstract object ReadScript(string filename, WareForm gui);