-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeDom.cs
More file actions
61 lines (55 loc) · 1.77 KB
/
Copy pathCodeDom.cs
File metadata and controls
61 lines (55 loc) · 1.77 KB
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
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
namespace helloworldCodeDom1
{
partial class Program
{
static void Main(string[] args)
{
CodeNamespace prgNamespace = BuildProgram();
var compilerOptions = new CodeGeneratorOptions()
{
IndentString = " ",
BracingStyle = "C",
BlankLinesBetweenMembers=false
};
var codeText = new StringBuilder();
using(var codeWriter = new StringWriter(codeText))
{
CodeDomProvider.CreateProvider("C#")
.GenerateCodeFromNamespace(prgNamespace, codeWriter, compilerOptions);
}
var script = codeText.ToString();
Console.WriteLine(script);
Console.ReadLine();
}
}
partial class Program
{
static CodeNamespace BuildProgram()
{
var ns = new CodeNamespace("MetaWorld");
var systemImport = new CodeNamespaceImport("System");
ns.Imports.Add(systemImport);
var programClass = new CodeTypeDeclaration("Program");
ns.Types.Add(programClass);
var methodMain = new CodeMemberMethod
{
Attributes = MemberAttributes.Static,
Name = "Main"
};
methodMain.Statements.Add
(
new CodeMethodInvokeExpression(
new CodeSnippetExpression("Console"),
"WriteLine",
new CodePrimitiveExpression("Hello, world!")
));
programClass.Members.Add(methodMain);
return ns;
}
}
}