Code control compile C # Program

Source: Internet
Author: User

Two days ago, I looked at MSDN and found an interesting feature. I can directly use code to control the compilation of C # programs. There is such a thing that it would be easier for some programs to implement the function of instantly modifying the code and then compiling the code into the assembly .... In other words, ASP. Net should not be implemented using this implementation, right?

First, compile the Code contained in the file referenced in the parameter. If no parameter is included, compile the Code:

 1:  using System;
 2:  using System.CodeDom;
 3:  using System.CodeDom.Compiler;
 4:  using Microsoft.CSharp;
 5:  using System.IO;
 6:   
 7:  namespace CSharpComplierControl
 8:  {
 9:      class Program
10:      {
11:          static void Main(string[] args)
12:          {
13:              string code = @"
14:  using System;
15:  namespace Application{
16:      class App{
17:          public static void Main(string[] args){
18:              Console.WriteLine(" + ""Hello,haha"" + @");
19:          }
20:      }
21:  }";
22:              bool noInput = false;
23:              FileInfo sourceCode = null;
24:              if (args.Length == 0)
25:              {
26:                  noInput = true;
27:              }
28:              else
29:              {
30:                  sourceCode = new FileInfo(args[0]);
31:                  if (!sourceCode.Exists)
32:                  {
33:                      noInput = true;
34:                  }
35:              }
36:   
37:              string objectExecutive = "test.exe";
38:              CompilerParameters compilerParameters = new CompilerParameters();
39:              compilerParameters.GenerateExecutable = true;
40:              compilerParameters.OutputAssembly = objectExecutive;
41:              compilerParameters.IncludeDebugInformation = true;
42:              compilerParameters.GenerateInMemory = false;
43:              compilerParameters.TreatWarningsAsErrors = false;
44:   
45:              CompilerResults compilerResults = null;
46:              if (noInput)
47:              {
48:                  compilerResults = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromSource(compilerParameters, code);
49:              }
50:              else
51:              {
52:                  compilerResults = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromFile(compilerParameters, sourceCode.FullName);
53:              }
54:              if (compilerResults.Errors.Count > 0)
55:       

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.