I am going to explain to create your own C# compiler using ASP.Net.
What is a compiler
A compiler is a program which used to processes statements (programming statement) written in specific programming language and convert this program into machine language.
Step 1 Add namespaces which used to provide classes and api to work with compilation and execution process
  1. using System.CodeDom.Compiler;
  2. using System.Collections.Specialized;
  3. using System.Diagnostics;
Step 2 Create a user interface which provide interface to users to writing code, checking errors, and buttons to compilations and executions.
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title></title>
  4. <style type="text/css">
  5. .style2
  6. {
  7. width: 178px;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <center>
  14. <div>
  15. <table>
  16. <tr>
  17. <td>
  18. <h3>Code Area</h3>
  19. <asp:TextBox ID="txtCode" runat="server" Height="347px" TextMode="MultiLine"
  20. Width="780px" BorderStyle="Solid" BorderWidth="1px">
  21. using System;
  22. class Test
  23. {
  24. public static void Main()
  25. {
  26. Console.WriteLine("Hello World");
  27. }
  28. }</asp:TextBox>
  29. </td>
  30. <td align="center" class="style2" valign="bottom">
  31. <asp:Button ID="btnCompiler" runat="server" Height="35px" Text="Compile"
  32. Width="150px" onclick="btnCompiler_Click" />
  33. <br />
  34. <br />
  35. <asp:Button ID="btnExecute" runat="server" Height="35px" Text="Execute"
  36. Width="150px" onclick="btnExecute_Click" />
  37. <br />
  38. <br />
  39. <asp:Button ID="btnReset" runat="server" Height="35px" Text="Reset"
  40. Width="150px" onclick="btnReset_Click" />
  41. <br />
  42. <br />
  43. <asp:Button ID="btnClose" runat="server" Height="35px" Text="Close"
  44. Width="150px" onclick="btnClose_Click" />
  45. </td>
  46. </tr>
  47. <tr>
  48. <td colspan="2"><h3>Result Area</h3>
  49. <p>
  50. <asp:ListBox ID="lstCompilerOutput" runat="server" Height="184px" Width="959px">
  51. </asp:ListBox>
  52. </p>
  53. </td>
  54. </tr>
  55. </table>
  56. </div>
  57. </center>
  58. </form>
  59. </body>
  60. </html>
Step 3 Switch your program to code file and create a method which used to define compilation language, return exception message if any and after successful compilation generate assembly as .exe.
  1. public CompilerResults CompileCode(string strCode)
  2. {
  3. CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp");
  4. CompilerParameters parameters = new CompilerParameters();
  5. parameters.GenerateExecutable = true;
  6. string path = Server.MapPath("Output/testFile.exe");
  7. parameters.OutputAssembly = path;
  8. parameters.GenerateInMemory = true;
  9. return codeDomProvider.CompileAssemblyFromSource(parameters, strCode);
  10. }
Note: Output is name of folder which used to store assembly. testFile.exe is name of assembly.
Step 4 Generate click event of btnCompiler button to call CompileCode method and display error message on user interface."
  1. lstCompilerOutput.Items.Clear();
  2. CompilerResults cResult = CompileCode(txtCode.Text);
  3. foreach (var item in cResult.Errors)
  4. {
  5. lstCompilerOutput.Items.Add(item.ToString());
  6. }
  7. if (cResult.Errors.Count == 0)
  8. {
  9. lstCompilerOutput.Items.Add("No Error");
  10. }
Step 5 No Error. mean your code is compile successfully without any error. For testing open your Output folder and check testFile.exe file is there.
Step 6 To Execute. Generate click event of btnExecute button to execute your created assembly as testFile.exe.
  1. string strCmdLine = Server.MapPath("Output/testFile.exe");
  2. Process proc = new Process();
  3. proc.StartInfo.CreateNoWindow = true;
  4. proc.StartInfo.UseShellExecute = true;
  5. proc.StartInfo.FileName = strCmdLine;
  6. proc.Start();
Step 7 Enjoy. Your compiler is ready to work.