private void button3_Click(object sender, EventArgs e) { CurrentTemplate = null; // Generate code for the template GeneratorResults razorResult = null; using (TextReader rdr = new StringReader(templateTextBox.Text)) { razorResult = _engine.GenerateCode(rdr); } CSharpCodeProvider codeProvider = new CSharpCodeProvider(); // Generate the code and put it in the text box: using (StringWriter sw = new StringWriter()) { codeProvider.GenerateCodeFromCompileUnit(razorResult.GeneratedCode, sw, new CodeGeneratorOptions()); generatedCodeTextBox.Text = sw.GetStringBuilder().ToString(); } // Compile the generated code into an assembly string outputAssemblyName = String.Format("Temp_{0}.dll", Guid.NewGuid().ToString("N")); CompilerResults results = codeProvider.CompileAssemblyFromDom( new CompilerParameters(new string[] { typeof(Form1).Assembly.CodeBase.Replace("file:///", "").Replace("/", "\\") }, outputAssemblyName), razorResult.GeneratedCode); if (results.Errors.HasErrors) { CompilerError err = results.Errors .OfType <CompilerError>() .Where(ce => !ce.IsWarning) .First(); MessageBox.Show(String.Format("Error Compiling Template: ({0}, {1}) {2}", err.Line, err.Column, err.ErrorText)); } else { // Load the assembly Assembly asm = Assembly.LoadFrom(outputAssemblyName); if (asm == null) { MessageBox.Show("Error loading template assembly"); } else { // Get the template type Type typ = asm.GetType("RazorOutput.Template"); if (typ == null) { MessageBox.Show("Could not find type RazorOutput.Template in assembly {0}", asm.FullName); } else { TemplateBase newTemplate = Activator.CreateInstance(typ) as TemplateBase; if (newTemplate == null) { MessageBox.Show("Could not construct RazorOutput.Template or it does not inherit from TemplateBase"); } else { CurrentTemplate = newTemplate; } } } } }
private void button3_Click(object sender, EventArgs e) { CurrentTemplate = null; // Generate code for the template GeneratorResults razorResult = null; using (TextReader rdr = new StringReader(templateTextBox.Text)) { razorResult = _engine.GenerateCode(rdr); } CSharpCodeProvider codeProvider = new CSharpCodeProvider(); // Generate the code and put it in the text box: using (StringWriter sw = new StringWriter()) { codeProvider.GenerateCodeFromCompileUnit(razorResult.GeneratedCode, sw, new CodeGeneratorOptions()); generatedCodeTextBox.Text = sw.GetStringBuilder().ToString(); } // Compile the generated code into an assembly string outputAssemblyName = String.Format("Temp_{0}.dll", Guid.NewGuid().ToString("N")); CompilerResults results = codeProvider.CompileAssemblyFromDom( new CompilerParameters(new string[] { typeof(Form1).Assembly.CodeBase.Replace("file:///", "").Replace("/", "\\") }, outputAssemblyName), razorResult.GeneratedCode); if (results.Errors.HasErrors) { CompilerError err = results.Errors .OfType<CompilerError>() .Where(ce => !ce.IsWarning) .First(); MessageBox.Show(String.Format("Error Compiling Template: ({0}, {1}) {2}", err.Line, err.Column, err.ErrorText)); } else { // Load the assembly Assembly asm = Assembly.LoadFrom(outputAssemblyName); if (asm == null) { MessageBox.Show("Error loading template assembly"); } else { // Get the template type Type typ = asm.GetType("RazorOutput.Template"); if (typ == null) { MessageBox.Show("Could not find type RazorOutput.Template in assembly {0}", asm.FullName); } else { TemplateBase newTemplate = Activator.CreateInstance(typ) as TemplateBase; if (newTemplate == null) { MessageBox.Show("Could not construct RazorOutput.Template or it does not inherit from TemplateBase"); } else { CurrentTemplate = newTemplate; } } } } }