public bool LoadData(ITestData test) { try { _testData = (TestData)test; //provide drawing attributes for parameters mroot = new MathNodeRoot(); if (_testData.Parameters.Count > 2) { mroot.ChildNodeCount = _testData.Parameters.Count; } parameters.Properties.Clear(); for (int i = 0; i < _testData.Parameters.Count; i++) { mroot[i] = (MathNode)_testData.Parameters[i].CloneExp(mroot); parameters.Properties.Add(new PropertySpec(_testData.Parameters[i].VariableName + ":" + _testData.Parameters[i].CodeVariableName, _testData.Parameters[i].VariableType.Type)); } foreach (IPropertyPointer pp in _testData.Pointers) { parameters.Properties.Add(new PropertySpec(pp.ToString() + ":" + pp.CodeName, pp.ObjectType)); //.PointerDataType)); } // CodeGeneratorOptions o = new CodeGeneratorOptions(); o.BlankLinesBetweenMembers = false; o.BracingStyle = "C"; o.ElseOnClosing = false; o.IndentString = " "; // CSharpCodeProvider cs = new CSharpCodeProvider(); StringWriter sw; sw = new StringWriter(); cs.GenerateCodeFromCompileUnit(_testData.CU, sw, o); // string sCode = sw.ToString(); sw.Close(); // int pos = sCode.IndexOf("a tool."); if (pos > 0) { sCode = sCode.Substring(0, pos) + "Limnor Visual Object Builder." + sCode.Substring(pos + 7); } // textBox1.Text = sCode; // CompilerParameters cp = new CompilerParameters(); foreach (AssemblyRef ar in _testData.Assemblies) { MathNode.AddImportLocation(ar.Location); } foreach (string s in MathNode.ImportLocations) { cp.ReferencedAssemblies.Add(s); } cp.GenerateExecutable = false; CompilerResults crs = cs.CompileAssemblyFromDom(cp, new CodeCompileUnit[] { _testData.CU }); if (crs.Errors.HasErrors) { MathNode.Trace("Error compiling."); MathNode.IndentIncrement(); FormCompilerError dlg = new FormCompilerError(); for (int i = 0; i < crs.Errors.Count; i++) { MathNode.Trace(crs.Errors[i].ToString()); dlg.AddItem(crs.Errors[i]); } MathNode.IndentDecrement(); dlg.TopLevel = false; dlg.Parent = this; dlg.Show(); dlg.TopMost = true; dlg.BringToFront(); } else { Type[] types = crs.CompiledAssembly.GetExportedTypes(); if (types != null) { for (int i = 0; i < types.Length; i++) { if (types[i].Name == _testData.ClassName) { _mi = types[i].GetMethod(_testData.MethodName); if (_mi != null) { break; } } } } } textBox2.Text = MathNode.GetLogContents(); return(true); } catch (Exception err) { MathNode.Log(this, err); textBox2.Text = MathNode.GetLogContents(); } return(false); }
public void compile() { StringCollection importLocations = new StringCollection(); CSharpCodeProvider cs = new CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(new string[] { }); cp.GenerateInMemory = true; cp.OutputAssembly = "AutoGenerated"; foreach (AssemblyRef ar in Imports) { importLocations.Add(ar.Location); } foreach (string s in importLocations) { cp.ReferencedAssemblies.Add(s); } cp.GenerateExecutable = false; if (DebugCompile) { CodeGeneratorOptions o = new CodeGeneratorOptions(); o.BlankLinesBetweenMembers = false; o.BracingStyle = "C"; o.ElseOnClosing = false; o.IndentString = " "; StringWriter sw = new StringWriter(); string sErr = ""; try { cs.GenerateCodeFromCompileUnit(CodeUnit, sw, o); } catch (Exception err) { sErr = err.Message; } sw.Flush(); sw.Close(); string sCode = sw.ToString(); if (string.IsNullOrEmpty(sErr)) { FormMessage.ShowMessage(null, sCode); } else { FormMessage.ShowMessage(null, string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\r\n{1}", sErr, sCode)); } } else { CompilerResults crs = cs.CompileAssemblyFromDom(cp, CodeUnit); if (crs.Errors.HasErrors) { FormCompilerError dlg = new FormCompilerError(); for (int i = 0; i < crs.Errors.Count; i++) { dlg.AddItem(crs.Errors[i]); } dlg.Show(); dlg.TopMost = true; dlg.BringToFront(); } else { Type[] types = crs.CompiledAssembly.GetExportedTypes(); if (types != null) { for (int i = 0; i < types.Length; i++) { if (types[i].Name == ClassName) { _mi = types[i].GetMethod(MethodName); if (_mi != null) { break; } } } } if (_mi == null) { FormCompilerError dlg = new FormCompilerError(); dlg.AddItem("Method not found in the compiled class"); dlg.Show(); dlg.TopMost = true; dlg.BringToFront(); } } } }