/// <summary> /// executes the template given a set of instance parameters /// and writes its output to the target file /// </summary> /// <param name="targetFile"></param> public void Execute(string targetFile, TemplateParameters parameters) { if (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(targetFile)) == false) { System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(targetFile)); } MyStreamWriter response = new MyStreamWriter(targetFile); Execute(response, parameters); response.Close(); // done writing }
/// <summary> /// executes the template and writes output to the response /// caller is required to open and close the output stream /// </summary> /// <param name="response">the output stream to write to</param> public void Execute(MyStreamWriter response, TemplateParameters parameters) { if (Result == null) { return; } if (Result.Errors.HasErrors) { foreach (System.CodeDom.Compiler.CompilerError error in Result.Errors) { response.Write(GenerateSource()); response.Write(String.Format("Error on line {0}: {1}", error.Line, error.ErrorText)); } return; } if (Sections == null) { return; // nothing to execute } Sections.Process(Result.CompiledAssembly, parameters, response); }
public void Process(Assembly assembly, TemplateParameters parameters, MyStreamWriter Response) { try { if (assembly == null) { return; } object instance = assembly.CreateInstance("Page"); Type typepage = instance.GetType(); // assign Response object to Page typepage.InvokeMember("me", BindingFlags.SetField, null, instance, new object[] { Response }); typepage.InvokeMember("Response", BindingFlags.SetField, null, instance, new object[] { Response }); // assign section texts to Page members for execution foreach (Section section in this) { if (section.Type == SectionType.Text) { // assign section text to Page members typepage.InvokeMember("SectionText" + section.Index, BindingFlags.SetField, null, instance, new object[] { section.Text }); } } // assign parameters to page if (parameters != null) { foreach (TemplateParameter tp in parameters) { // try to assign the parameter. if the parameter/variable is not defined in the // template then skip it, there is no need to break the execution here try { typepage.InvokeMember(tp.Name, BindingFlags.SetField, null, instance, new object[] { tp.Value }); } catch (MissingFieldException) { //variable was not declared in template but continue anyway } } } typepage.InvokeMember("RenderPage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, instance, null); } catch (Exception ex) { Exception inner; Response.WriteLine(); inner = ex; while (inner != null) { Response.WriteLine(inner.Message); inner = inner.InnerException; } Response.WriteLine(ex.StackTrace); } }