1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| namespace ConsoleApp1 { using System; using System.CodeDom; using System.CodeDom.Compiler; using System.IO; using System.Text.RegularExpressions;
class Program { static string providerName = "cs"; static string sourceFileName = "test.cs"; static void Main(string[] args) { CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);
LogMessage("Building CodeDOM graph...");
CodeCompileUnit cu = new CodeCompileUnit();
cu = BuildHelloWorldGraph();
StreamWriter sourceFile = new StreamWriter(sourceFileName); provider.GenerateCodeFromCompileUnit(cu, sourceFile, null); sourceFile.Close();
CompilerParameters opt = new CompilerParameters(new string[]{ "System.dll" }); opt.GenerateExecutable = true; opt.OutputAssembly = "HelloWorld.exe"; opt.TreatWarningsAsErrors = true; opt.IncludeDebugInformation = true; opt.GenerateInMemory = true; opt.CompilerOptions = "/doc:HelloWorldDoc.xml";
CompilerResults results;
LogMessage("Compiling with " + providerName); results = provider.CompileAssemblyFromFile(opt, sourceFileName);
OutputResults(results); if (results.NativeCompilerReturnValue != 0) { LogMessage(""); LogMessage("Compilation failed."); } else { LogMessage(""); LogMessage("Demo completed successfully."); }
Console.ReadKey(); }
public static CodeCompileUnit BuildHelloWorldGraph() { CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace samples = new CodeNamespace("Samples"); compileUnit.Namespaces.Add(samples);
samples.Imports.Add(new CodeNamespaceImport("System"));
CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
class1.Comments.Add(new CodeCommentStatement("<summary>", true)); class1.Comments.Add(new CodeCommentStatement( "Create a Hello World application.", true)); class1.Comments.Add(new CodeCommentStatement( @"<seealso cref=" + '"' + "Class1.Main" + '"' + "/>", true)); class1.Comments.Add(new CodeCommentStatement("</summary>", true));
samples.Types.Add(class1);
CodeEntryPointMethod start = new CodeEntryPointMethod(); start.Comments.Add(new CodeCommentStatement("<summary>", true)); start.Comments.Add(new CodeCommentStatement( "Main method for HelloWorld application.", true)); start.Comments.Add(new CodeCommentStatement( @"<para>Add a new paragraph to the description.</para>", true)); start.Comments.Add(new CodeCommentStatement("</summary>", true));
CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console");
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( csSystemConsoleType, "WriteLine", new CodePrimitiveExpression("Hello World!"));
start.Statements.Add(cs1);
CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression( csSystemConsoleType, "WriteLine", new CodePrimitiveExpression( "Press the ENTER key to continue."));
start.Statements.Add(cs2);
CodeMethodInvokeExpression csReadLine = new CodeMethodInvokeExpression(csSystemConsoleType, "ReadLine");
start.Statements.Add(csReadLine);
class1.Members.Add(start);
return compileUnit; } static void LogMessage(string text) { Console.WriteLine(text); }
static void OutputResults(CompilerResults results) { LogMessage("NativeCompilerReturnValue=" + results.NativeCompilerReturnValue.ToString()); foreach (string s in results.Output) { LogMessage(s); } } } }
|