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: