/// <summary> /// /// </summary> public void MakeModule() { try { CompilerParameters cp = new CompilerParameters { GenerateExecutable = false, IncludeDebugInformation = false }; foreach ( var path in AppDomain.CurrentDomain.GetAssemblies() .Where( asm => !asm.IsDynamic && !asm.Location.Contains("mscorlib") && !string.IsNullOrEmpty(asm.Location)) .Select(asm => asm.Location)) { cp.ReferencedAssemblies.Add(path); } CompilerResults results = Compiler.Compile(txtCode.Text, cp); bool hasErrors = false; if (results.Errors.Count > 0) { StringBuilder builder = new StringBuilder(); //"Compilation messages:\r\n"); foreach (CompilerError err in results.Errors) { hasErrors |= !err.IsWarning; if (err.Line > 0) { builder.AppendFormat("Line {0}, col {1}: ", err.Line, err.Column); } if (!string.IsNullOrEmpty(err.ErrorNumber)) { builder.AppendFormat("[{0}] ", err.ErrorNumber); } builder.Append(err.ErrorText); builder.Append("\r\n"); } using (CustomModuleErrors error = new CustomModuleErrors()) { error.ErrorText = builder.ToString(); error.Text = "Compilation " + (hasErrors ? "errors" : "warnings"); error.ShowDialog(this); } if (hasErrors) { Module = null; return; } } foreach (Type t in results.CompiledAssembly.GetTypes().Where(t => t.GetInterface("IModule") != null)) { Module = (IModule)Activator.CreateInstance(t, Program.AWB); } } catch (Exception ex) { Module = null; ErrorHandler.HandleException(ex); } }
/// <summary> /// /// </summary> public void MakeModule() { try { CompilerParameters cp = new CompilerParameters { GenerateExecutable = false, IncludeDebugInformation = false }; foreach ( var path in AppDomain.CurrentDomain.GetAssemblies() .Where( asm => !asm.Location.Contains("mscorlib") && !string.IsNullOrEmpty(asm.Location)) .Select(asm => asm.Location)) { cp.ReferencedAssemblies.Add(path); } CompilerResults results = Compiler.Compile(txtCode.Text, cp); bool hasErrors = false; if (results.Errors.Count > 0) { StringBuilder builder = new StringBuilder(); //"Compilation messages:\r\n"); foreach (CompilerError err in results.Errors) { hasErrors |= !err.IsWarning; if (err.Line > 0) builder.AppendFormat("Line {0}, col {1}: ", err.Line, err.Column); if (!string.IsNullOrEmpty(err.ErrorNumber)) builder.AppendFormat("[{0}] ", err.ErrorNumber); builder.Append(err.ErrorText); builder.Append("\r\n"); } using (CustomModuleErrors error = new CustomModuleErrors()) { error.ErrorText = builder.ToString(); error.Text = "Compilation " + (hasErrors ? "errors" : "warnings"); error.ShowDialog(this); } if (hasErrors) { Module = null; return; } } foreach (Type t in results.CompiledAssembly.GetTypes().Where(t => t.GetInterface("IModule") != null)) { Module = (IModule) Activator.CreateInstance(t, Program.AWB); } } catch (Exception ex) { Module = null; ErrorHandler.HandleException(ex); } }
private void button1_Click(object sender, EventArgs e) { textBox2.Clear(); string code = @"using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using WikiFunctions; using System.Linq; namespace CSharpEvaluator { class CSharpEval { public object EvalCode() { return " + textBox1.Text + @"; } } } "; CSharpCodeProvider c = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v3.5" } }); CompilerParameters cp = new CompilerParameters { GenerateExecutable = false, IncludeDebugInformation = false }; // Microsoft.GeneratedCode check is for Mono compatibility foreach ( var path in AppDomain.CurrentDomain.GetAssemblies() .Where( asm => !asm.FullName.Contains("Microsoft.GeneratedCode") && !asm.Location.Contains("mscorlib") && !string.IsNullOrEmpty(asm.Location)) .Select(asm => asm.Location)) { cp.ReferencedAssemblies.Add(path); } CompilerResults results = c.CompileAssemblyFromSource(cp, code); if (results.Errors.Count > 0) { bool hasErrors = false; StringBuilder builder = new StringBuilder(); // "Compilation messages:\r\n"); foreach (CompilerError err in results.Errors) { hasErrors |= !err.IsWarning; if (err.Line > 0) builder.AppendFormat("Line {0}, col {1}: ", err.Line, err.Column); if (!string.IsNullOrEmpty(err.ErrorNumber)) builder.AppendFormat("[{0}] ", err.ErrorNumber); builder.Append(err.ErrorText); builder.Append("\r\n"); } using (CustomModuleErrors error = new CustomModuleErrors()) { error.ErrorText = builder.ToString(); error.Text = "Compilation " + (hasErrors ? "errors" : "warnings"); error.ShowDialog(this); } if (hasErrors) { return; } } Assembly a = results.CompiledAssembly; object o = a.CreateInstance("CSharpEvaluator.CSharpEval"); Type t = o.GetType(); MethodInfo mi = t.GetMethod("EvalCode"); object s = mi.Invoke(o, null); textBox2.Text = s.ToString(); }
private void button1_Click(object sender, EventArgs e) { textBox2.Clear(); string code = @"using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using WikiFunctions; using System.Linq; namespace CSharpEvaluator { class CSharpEval { public object EvalCode() { return " + textBox1.Text + @"; } } } "; CSharpCodeProvider c = new CSharpCodeProvider(new Dictionary <string, string> { { "CompilerVersion", "v3.5" } }); CompilerParameters cp = new CompilerParameters { GenerateExecutable = false, IncludeDebugInformation = false }; // Microsoft.GeneratedCode check is for Mono compatibility foreach ( var path in AppDomain.CurrentDomain.GetAssemblies() .Where( asm => !asm.FullName.Contains("Microsoft.GeneratedCode") && !asm.Location.Contains("mscorlib") && !string.IsNullOrEmpty(asm.Location)) .Select(asm => asm.Location)) { cp.ReferencedAssemblies.Add(path); } CompilerResults results = c.CompileAssemblyFromSource(cp, code); if (results.Errors.Count > 0) { bool hasErrors = false; StringBuilder builder = new StringBuilder(); // "Compilation messages:\r\n"); foreach (CompilerError err in results.Errors) { hasErrors |= !err.IsWarning; if (err.Line > 0) { builder.AppendFormat("Line {0}, col {1}: ", err.Line, err.Column); } if (!string.IsNullOrEmpty(err.ErrorNumber)) { builder.AppendFormat("[{0}] ", err.ErrorNumber); } builder.Append(err.ErrorText); builder.Append("\r\n"); } using (CustomModuleErrors error = new CustomModuleErrors()) { error.ErrorText = builder.ToString(); error.Text = "Compilation " + (hasErrors ? "errors" : "warnings"); error.ShowDialog(this); } if (hasErrors) { return; } } Assembly a = results.CompiledAssembly; object o = a.CreateInstance("CSharpEvaluator.CSharpEval"); Type t = o.GetType(); MethodInfo mi = t.GetMethod("EvalCode"); object s = mi.Invoke(o, null); textBox2.Text = s.ToString(); }