- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.CodeDom.Compiler;
- using System.Reflection;
- using Microsoft.CSharp;
- namespace FUDBuilder
- {
- public partial class frmMain : Form
- {
- public frmMain()
- {
- InitializeComponent();
- }
- private void frmMain_Load(object sender, EventArgs e)
- {
- // some sample code that shows a message box
- string code = "using System; using System.Windows.Forms;\nnamespace TestCode { class TestClass { static void Main() { MessageBox.Show(\"Hello, runtime compiled world!\"); } } }";
- Compile(code, "compiled.exe");
- }
- private void Compile(string code, string fileName)
- {
- cparams.GenerateExecutable = true; // we want to build an EXE
- cparams.GenerateInMemory = false; // don't generate this in memory
- cparams.OutputAssembly = fileName; // set the output file name
- cparams.ReferencedAssemblies.Add("System.dll");
- cparams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
- cparams.CompilerOptions = "/optimize /target:winexe"; // optimise and set the target to a WinForms application (change to /target:exe if you want console)
- // compile
- CompilerResults result = compiler.CompileAssemblyFromSource(cparams, code);
- // if there are errors in the code, tell the user
- foreach (CompilerError error in result.Errors)
- {
- MessageBox.Show(string.Format("Line {0}: Error {1} - {2}", error.Line, error.ErrorNumber, error.ErrorText));
- }
- }
- }
- }
Code Compiler (C#)
Posted by Anonymous on Thu 25th Nov 2010 18:06
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.