-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcodedirective.cs
More file actions
214 lines (185 loc) · 8.58 KB
/
codedirective.cs
File metadata and controls
214 lines (185 loc) · 8.58 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//<Snippet1>
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Globalization;
namespace System.CodeDom
{
class CodeDirectiveDemo
{
static void Main()
{
try
{
DemonstrateCodeDirectives("cs", "ChecksumPragma.cs", "ChecksumPragmaCS.exe");
}
catch (Exception e)
{
Console.WriteLine("Unexpected Exception:" + e.ToString());
}
}
// Create and compile code containing code directives.
static void DemonstrateCodeDirectives(string providerName, string sourceFileName, string assemblyName)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider(providerName);
Console.WriteLine("Building the CodeDOM graph...");
CodeCompileUnit cu = new CodeCompileUnit();
CreateGraph(cu);
StringWriter sw = new StringWriter();
Console.WriteLine("Generating code...");
provider.GenerateCodeFromCompileUnit(cu, sw, null);
string output = sw.ToString();
output = Regex.Replace(output, "Runtime Version:[^\r\n]*",
"Runtime Version omitted for demo");
Console.WriteLine("Dumping source code...");
Console.WriteLine(output);
Console.WriteLine("Writing source code to file...");
Stream s = File.Open(sourceFileName, FileMode.Create);
StreamWriter t = new StreamWriter(s);
t.Write(output);
t.Close();
s.Close();
CompilerParameters opt = new CompilerParameters(new string[]{
"System.dll",
"System.Xml.dll",
"System.Windows.Forms.dll",
"System.Data.dll",
"System.Drawing.dll"});
opt.GenerateExecutable = false;
opt.TreatWarningsAsErrors = true;
opt.IncludeDebugInformation = true;
opt.GenerateInMemory = true;
CompilerResults results;
Console.WriteLine("Compiling with " + providerName);
results = provider.CompileAssemblyFromFile(opt, sourceFileName);
OutputResults(results);
if (results.NativeCompilerReturnValue != 0)
{
Console.WriteLine("");
Console.WriteLine("Compilation failed.");
}
else
{
Console.WriteLine("");
Console.WriteLine("Demo complete.");
}
File.Delete(sourceFileName);
}
// This example uses the SHA1 and MD5 algorithms.
// Due to collision problems with SHA1 and MD5, Microsoft recommends using a NIST-approved hash function.
private static Guid HashMD5 = new Guid(0x406ea660, 0x64cf, 0x4c82, 0xb6, 0xf0, 0x42, 0xd4, 0x81, 0x72, 0xa7, 0x99);
private static Guid HashSHA1 = new Guid(0xff1816ec, 0xaa5e, 0x4d10, 0x87, 0xf7, 0x6f, 0x49, 0x63, 0x83, 0x34, 0x60);
// Create a CodeDOM graph.
static void CreateGraph( CodeCompileUnit cu)
{
//<Snippet2>
cu.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
"Compile Unit Region"));
//</Snippet2>
//<Snippet3>
cu.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
string.Empty));
//</Snippet3>
//<Snippet4>
CodeChecksumPragma pragma1 = new CodeChecksumPragma();
//</Snippet4>
//<Snippet5>
pragma1.FileName = "c:\\temp\\test\\OuterLinePragma.txt";
//</Snippet5>
//<Snippet6>
pragma1.ChecksumAlgorithmId = HashMD5;
//</Snippet6>
//<Snippet7>
pragma1.ChecksumData = new byte[] { 0xAA, 0xAA };
//</Snippet7>
cu.StartDirectives.Add(pragma1);
//<Snippet8>
CodeChecksumPragma pragma2 = new CodeChecksumPragma("test.txt", HashSHA1, new byte[] { 0xBB, 0xBB, 0xBB });
//</Snippet8>
cu.StartDirectives.Add(pragma2);
CodeNamespace ns = new CodeNamespace("Namespace1");
ns.Imports.Add(new CodeNamespaceImport("System"));
ns.Imports.Add(new CodeNamespaceImport("System.IO"));
cu.Namespaces.Add(ns);
ns.Comments.Add(new CodeCommentStatement("Namespace Comment"));
CodeTypeDeclaration cd = new CodeTypeDeclaration("Class1");
ns.Types.Add(cd);
cd.Comments.Add(new CodeCommentStatement("Outer Type Comment"));
cd.LinePragma = new CodeLinePragma("c:\\temp\\test\\OuterLinePragma.txt", 300);
CodeMemberMethod method1 = new CodeMemberMethod();
method1.Name = "Method1";
method1.Attributes = (method1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
CodeMemberMethod method2 = new CodeMemberMethod();
method2.Name = "Method2";
method2.Attributes = (method2.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
method2.Comments.Add(new CodeCommentStatement("Method 2 Comment"));
cd.Members.Add(method1);
cd.Members.Add(method2);
cd.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start,
"Outer Type Region"));
cd.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End,
string.Empty));
CodeMemberField field1 = new CodeMemberField(typeof(String), "field1");
cd.Members.Add(field1);
field1.Comments.Add(new CodeCommentStatement("Field 1 Comment"));
//<Snippet9>
CodeRegionDirective codeRegionDirective1 = new CodeRegionDirective(CodeRegionMode.Start,
"Field Region");
//</Snippet9>
//<Snippet10>
field1.StartDirectives.Add(codeRegionDirective1);
//</Snippet10>
CodeRegionDirective codeRegionDirective2 = new CodeRegionDirective(CodeRegionMode.End,
"");
//<Snippet11>
codeRegionDirective2.RegionMode = CodeRegionMode.End;
//</Snippet11>
//<Snippet12>
codeRegionDirective2.RegionText = string.Empty;
//</Snippet12>
//<Snippet13>
field1.EndDirectives.Add(codeRegionDirective2);
//</Snippet13>
//<Snippet16>
CodeSnippetStatement snippet1 = new CodeSnippetStatement();
snippet1.Value = " Console.WriteLine(field1);";
CodeRegionDirective regionStart = new CodeRegionDirective(CodeRegionMode.End, "");
regionStart.RegionText = "Snippet Region";
regionStart.RegionMode = CodeRegionMode.Start;
snippet1.StartDirectives.Add(regionStart);
snippet1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
//</Snippet16>
// CodeStatement example
CodeConstructor constructor1 = new CodeConstructor();
constructor1.Attributes = (constructor1.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Public;
CodeStatement codeAssignStatement1 = new CodeAssignStatement(
new CodeFieldReferenceExpression(
new CodeThisReferenceExpression(),
"field1"),
new CodePrimitiveExpression("value1"));
//<Snippet14>
codeAssignStatement1.StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, "Statements Region"));
//</Snippet14>
cd.Members.Add(constructor1);
//<Snippet15>
codeAssignStatement1.EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, string.Empty));
//</Snippet15>
method2.Statements.Add(codeAssignStatement1);
method2.Statements.Add(snippet1);
}
static void OutputResults(CompilerResults results)
{
Console.WriteLine("NativeCompilerReturnValue=" +
results.NativeCompilerReturnValue.ToString());
foreach (string s in results.Output)
{
Console.WriteLine(s);
}
}
}
}
//</Snippet1>