2 // Microsoft.CSharp.CSharpCodeProviderTest.cs
5 // Gert Driesen (drieseng@users.sourceforge.net)
12 using System.CodeDom.Compiler;
13 using System.Collections.Specialized;
14 using System.Globalization;
16 using System.Reflection;
17 using Microsoft.CSharp;
18 using NUnit.Framework;
20 namespace MonoTests.Microsoft.CSharp
23 public class CSharpCodeProviderTest
25 private string _tempDir;
26 private CodeDomProvider _codeProvider;
28 private static readonly string _sourceLibrary1 = "public class Test1 {}";
29 private static readonly string _sourceLibrary2 = "public class Test2 {}";
30 private static readonly string _sourceExecutable = "public class Program { static void Main () { } }";
35 _codeProvider = new CSharpCodeProvider ();
36 _tempDir = CreateTempDirectory ();
40 public void TearDown ()
42 RemoveDirectory (_tempDir);
46 public void FileExtension ()
48 Assert.AreEqual ("cs", _codeProvider.FileExtension);
52 public void LanguageOptionsTest ()
54 Assert.AreEqual (LanguageOptions.None, _codeProvider.LanguageOptions);
58 public void GeneratorSupports ()
60 ICodeGenerator codeGenerator = _codeProvider.CreateGenerator ();
61 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#1");
62 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ArraysOfArrays), "#2");
63 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.AssemblyAttributes), "#3");
64 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ChainedConstructorArguments), "#4");
65 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ComplexExpressions), "#5");
66 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareDelegates), "#6");
67 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEnums), "#7");
68 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareEvents), "#8");
69 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareInterfaces), "#9");
70 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareValueTypes), "#10");
71 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.EntryPointMethod), "#11");
72 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GotoStatements), "#12");
73 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultidimensionalArrays), "#13");
74 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.MultipleInterfaceMembers), "#14");
75 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.NestedTypes), "#15");
76 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ParameterAttributes), "#16");
77 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PublicStaticMembers), "#17");
78 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReferenceParameters), "#18");
79 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.ReturnTypeAttributes), "#19");
80 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.StaticConstructors), "#20");
81 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.TryCatchStatements), "#21");
82 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Win32Resources), "#22");
83 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.DeclareIndexerProperties), "#23");
84 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeDeclaration), "#24");
85 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.GenericTypeReference), "#25");
86 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.PartialTypes), "#26");
87 Assert.IsTrue (codeGenerator.Supports (GeneratorSupport.Resources), "#27");
91 public void CompileFromFile_InMemory ()
94 string sourceFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
95 using (FileStream f = new FileStream (sourceFile, FileMode.Create)) {
96 using (StreamWriter s = new StreamWriter (f)) {
97 s.Write (_sourceLibrary1);
103 CompilerParameters options = new CompilerParameters ();
104 options.GenerateExecutable = false;
105 options.GenerateInMemory = true;
106 options.TempFiles = new TempFileCollection (_tempDir);
107 options.EmbeddedResources.Add (sourceFile);
109 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
110 CompilerResults results = compiler.CompileAssemblyFromFile (options,
113 // verify compilation was successful
114 AssertCompileResults (results, true);
116 Assembly compiledAssembly = results.CompiledAssembly;
118 Assert.IsNotNull (compiledAssembly, "#1");
119 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#2");
120 Assert.IsNull (results.PathToAssembly, "#3");
121 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#4");
123 // verify we don't cleanup files in temp directory too agressively
124 string[] tempFiles = Directory.GetFiles (_tempDir);
125 Assert.AreEqual (1, tempFiles.Length, "#5");
126 Assert.AreEqual (sourceFile, tempFiles[0], "#6");
128 string[] resources = compiledAssembly.GetManifestResourceNames();
129 Assert.IsNotNull (resources, "#7");
130 Assert.AreEqual (1, resources.Length, "#8");
131 Assert.AreEqual ("file.cs", resources[0], "#9");
132 Assert.IsNull (compiledAssembly.GetFile ("file.cs"), "#10");
133 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file.cs"), "#11");
134 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file.cs");
135 Assert.IsNotNull (info, "#12");
136 Assert.IsNull (info.FileName, "#13");
137 Assert.IsNull (info.ReferencedAssembly, "#14");
138 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#15");
142 public void CompileFromFileBatch_Executable_InMemory ()
144 // create source file
145 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
146 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
147 using (StreamWriter s = new StreamWriter (f)) {
148 s.Write (_sourceLibrary1);
154 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
155 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
156 using (StreamWriter s = new StreamWriter (f)) {
157 s.Write (_sourceExecutable);
163 CompilerParameters options = new CompilerParameters ();
164 options.GenerateExecutable = true;
165 options.GenerateInMemory = true;
166 options.OutputAssembly = string.Empty;
167 options.TempFiles = new TempFileCollection (_tempDir);
168 options.EmbeddedResources.Add (sourceFile1);
169 options.LinkedResources.Add (sourceFile2);
171 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
172 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
173 new string [] { sourceFile1, sourceFile2 });
175 // verify compilation was successful
176 AssertCompileResults (results, true);
178 Assembly compiledAssembly = results.CompiledAssembly;
180 Assert.IsNotNull (compiledAssembly, "#A1");
181 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
182 Assert.IsNull (results.PathToAssembly, "#A3");
183 Assert.IsNotNull (options.OutputAssembly, "#A4");
184 Assert.AreEqual (".exe", Path.GetExtension (options.OutputAssembly), "#A5");
185 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
186 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
188 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
189 Assert.IsNotNull (compiledAssembly.GetType ("Program"), "#B2");
191 // verify we don't cleanup files in temp directory too agressively
192 string [] tempFiles = Directory.GetFiles (_tempDir);
193 Assert.AreEqual (2, tempFiles.Length, "#C1");
194 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
195 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
197 string[] resources = compiledAssembly.GetManifestResourceNames();
198 Assert.IsNotNull (resources, "#D1");
199 Assert.AreEqual (2, resources.Length, "#D2");
201 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
202 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
203 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
204 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
205 Assert.IsNotNull (info, "#E4");
206 Assert.IsNull (info.FileName, "#E5");
207 Assert.IsNull (info.ReferencedAssembly, "#E6");
208 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
210 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
212 compiledAssembly.GetFile ("file2.cs");
214 } catch (FileNotFoundException) {
217 compiledAssembly.GetManifestResourceStream ("file2.cs");
219 } catch (FileNotFoundException) {
221 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
222 Assert.IsNotNull (info, "#F4");
223 Assert.IsNotNull (info.FileName, "#F5");
224 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
225 Assert.IsNull (info.ReferencedAssembly, "#F7");
226 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
230 public void CompileFromFileBatch_Library_InMemory ()
232 // create source file
233 string sourceFile1 = Path.Combine (_tempDir, "file1." + _codeProvider.FileExtension);
234 using (FileStream f = new FileStream (sourceFile1, FileMode.Create)) {
235 using (StreamWriter s = new StreamWriter (f)) {
236 s.Write (_sourceLibrary1);
242 string sourceFile2 = Path.Combine (_tempDir, "file2." + _codeProvider.FileExtension);
243 using (FileStream f = new FileStream (sourceFile2, FileMode.Create)) {
244 using (StreamWriter s = new StreamWriter (f)) {
245 s.Write (_sourceLibrary2);
251 CompilerParameters options = new CompilerParameters ();
252 options.GenerateExecutable = false;
253 options.GenerateInMemory = true;
254 options.TempFiles = new TempFileCollection (_tempDir);
255 options.EmbeddedResources.Add (sourceFile1);
256 options.LinkedResources.Add (sourceFile2);
258 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
259 CompilerResults results = compiler.CompileAssemblyFromFileBatch (options,
260 new string [] { sourceFile1, sourceFile2 });
262 // verify compilation was successful
263 AssertCompileResults (results, true);
265 Assembly compiledAssembly = results.CompiledAssembly;
267 Assert.IsNotNull (compiledAssembly, "#A1");
268 Assert.AreEqual (string.Empty, compiledAssembly.Location, "#A2");
269 Assert.IsNull (results.PathToAssembly, "#A3");
270 Assert.IsNotNull (options.OutputAssembly, "#A4");
271 Assert.AreEqual (".dll", Path.GetExtension (options.OutputAssembly), "#A5");
272 Assert.AreEqual (_tempDir, Path.GetDirectoryName (options.OutputAssembly), "#A6");
273 Assert.IsFalse (File.Exists (options.OutputAssembly), "#A7");
275 Assert.IsNotNull (compiledAssembly.GetType ("Test1"), "#B1");
276 Assert.IsNotNull (compiledAssembly.GetType ("Test2"), "#B2");
278 // verify we don't cleanup files in temp directory too agressively
279 string [] tempFiles = Directory.GetFiles (_tempDir);
280 Assert.AreEqual (2, tempFiles.Length, "#C1");
281 Assert.IsTrue (File.Exists (sourceFile1), "#C2");
282 Assert.IsTrue (File.Exists (sourceFile2), "#C3");
284 string[] resources = compiledAssembly.GetManifestResourceNames();
285 Assert.IsNotNull (resources, "#D1");
286 Assert.AreEqual (2, resources.Length, "#D2");
288 Assert.IsTrue (resources[0] == "file1.cs" || resources [0] == "file2.cs", "#E1");
289 Assert.IsNull (compiledAssembly.GetFile ("file1.cs"), "#E2");
290 Assert.IsNotNull (compiledAssembly.GetManifestResourceStream ("file1.cs"), "#E3");
291 ManifestResourceInfo info = compiledAssembly.GetManifestResourceInfo ("file1.cs");
292 Assert.IsNotNull (info, "#E4");
293 Assert.IsNull (info.FileName, "#E5");
294 Assert.IsNull (info.ReferencedAssembly, "#E6");
295 Assert.AreEqual ((ResourceLocation.Embedded | ResourceLocation.ContainedInManifestFile), info.ResourceLocation, "#E7");
297 Assert.IsTrue (resources[1] == "file1.cs" || resources [1] == "file2.cs", "#F1");
299 compiledAssembly.GetFile ("file2.cs");
301 } catch (FileNotFoundException) {
304 compiledAssembly.GetManifestResourceStream ("file2.cs");
306 } catch (FileNotFoundException) {
308 info = compiledAssembly.GetManifestResourceInfo ("file2.cs");
309 Assert.IsNotNull (info, "#F4");
310 Assert.IsNotNull (info.FileName, "#F5");
311 Assert.AreEqual ("file2.cs", info.FileName, "#F6");
312 Assert.IsNull (info.ReferencedAssembly, "#F7");
313 Assert.AreEqual ((ResourceLocation) 0, info.ResourceLocation, "#F8");
317 public void CompileFromSource_InMemory ()
319 // create a file in temp directory to ensure that compiler is not removing
320 // too much (temporary) files
321 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
322 using (FileStream fs = File.Create (tempFile)) {
326 CompilerParameters options = new CompilerParameters ();
327 options.GenerateExecutable = false;
328 options.GenerateInMemory = true;
329 options.TempFiles = new TempFileCollection (_tempDir);
331 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
332 CompilerResults results = compiler.CompileAssemblyFromSource (options,
335 // verify compilation was successful
336 AssertCompileResults (results, true);
338 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
339 Assert.IsNull (results.PathToAssembly, "#2");
340 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#3");
342 // verify we don't cleanup files in temp directory too agressively
343 string[] tempFiles = Directory.GetFiles (_tempDir);
344 Assert.AreEqual (1, tempFiles.Length, "#4");
345 Assert.AreEqual (tempFile, tempFiles[0], "#5");
349 public void CompileFromSource_InMemory_Twice ()
351 CompilerParameters options = new CompilerParameters ();
352 options.GenerateExecutable = false;
353 options.GenerateInMemory = true;
355 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
357 var src_1 = "class X { ";
359 CompilerResults results_1 = compiler.CompileAssemblyFromSource (options, src_1);
360 var output_1 = options.OutputAssembly;
362 var src_2 = "class X { }";
364 CompilerResults results_2 = compiler.CompileAssemblyFromSource (options, src_2);
365 var output_2 = options.OutputAssembly;
367 // verify compilation was successful
368 AssertCompileResults (results_2, true);
370 Assert.AreEqual (output_1, output_2, "#1");
375 public void CompileFromSource_InMemory_With_Extra_Delete ()
377 CompilerParameters options = new CompilerParameters ();
378 options.GenerateExecutable = false;
379 options.GenerateInMemory = true;
381 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
383 var src_1 = "class X { ";
385 compiler.CompileAssemblyFromSource (options, src_1);
387 options.TempFiles.Delete ();
388 options.TempFiles.Delete ();
392 public void CompileFromSourceBatch_InMemory ()
394 // create a file in temp directory to ensure that compiler is not removing
395 // too much (temporary) files
396 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
397 using (FileStream fs = File.Create (tempFile)) {
401 string outputAssembly = Path.Combine (_tempDir, "sourcebatch.dll");
403 CompilerParameters options = new CompilerParameters ();
404 options.GenerateExecutable = false;
405 options.GenerateInMemory = true;
406 options.OutputAssembly = outputAssembly;
407 options.TempFiles = new TempFileCollection (_tempDir);
409 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
410 CompilerResults results = compiler.CompileAssemblyFromSourceBatch (options,
411 new string [] { _sourceLibrary1, _sourceLibrary2 });
413 // verify compilation was successful
414 AssertCompileResults (results, true);
416 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#A1");
417 Assert.IsNull (results.PathToAssembly, "#A2");
418 Assert.IsNotNull (options.OutputAssembly, "#A3");
419 Assert.AreEqual (outputAssembly, options.OutputAssembly, "#A4");
420 Assert.IsTrue (File.Exists (outputAssembly), "#A5");
422 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test1"), "#B1");
423 Assert.IsNotNull (results.CompiledAssembly.GetType ("Test2"), "#B2");
425 // verify we don't cleanup files in temp directory too agressively
426 string[] tempFiles = Directory.GetFiles (_tempDir);
427 Assert.AreEqual (2, tempFiles.Length, "#C1");
428 Assert.AreEqual (tempFile, tempFiles[0], "#C2");
429 Assert.AreEqual (outputAssembly, tempFiles [1], "#C3");
433 public void CompileFromDom_NotInMemory ()
435 // create a file in temp directory to ensure that compiler is not removing
436 // too much (temporary) files
437 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
438 using (FileStream fs = File.Create (tempFile)) {
442 // compile and verify result in separate appdomain to avoid file locks
443 AppDomain testDomain = CreateTestDomain ();
444 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
446 string outputAssembly = null;
449 outputAssembly = compileTester.CompileAssemblyFromDom (_tempDir);
451 AppDomain.Unload (testDomain);
454 // there should be two files in temp dir: temp file and output assembly
455 string[] tempFiles = Directory.GetFiles (_tempDir);
456 Assert.AreEqual (2, tempFiles.Length, "#1");
457 Assert.IsTrue (File.Exists (outputAssembly), "#2");
458 Assert.IsTrue (File.Exists (tempFile), "#3");
462 public void CompileFromDomBatch_NotInMemory ()
464 // create a file in temp directory to ensure that compiler is not removing
465 // too much (temporary) files
466 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
467 using (FileStream fs = File.Create (tempFile)) {
471 // compile and verify result in separate appdomain to avoid file locks
472 AppDomain testDomain = CreateTestDomain ();
473 CrossDomainTester compileTester = CreateCrossDomainTester (testDomain);
475 string outputAssembly = null;
477 outputAssembly = compileTester.CompileAssemblyFromDomBatch (_tempDir);
479 AppDomain.Unload (testDomain);
482 // there should be two files in temp dir: temp file and output assembly
483 string[] tempFiles = Directory.GetFiles (_tempDir);
484 Assert.AreEqual (2, tempFiles.Length, "#1");
485 Assert.IsTrue (File.Exists (outputAssembly), "#2");
486 Assert.IsTrue (File.Exists (tempFile), "#3");
490 public void CompileFromDom_InMemory ()
492 // create a file in temp directory to ensure that compiler is not removing
493 // too much (temporary) files
494 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
495 using (FileStream fs = File.Create (tempFile)) {
499 CompilerParameters options = new CompilerParameters ();
500 options.GenerateExecutable = false;
501 options.GenerateInMemory = true;
502 options.TempFiles = new TempFileCollection (_tempDir);
504 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
505 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
507 // verify compilation was successful
508 AssertCompileResults (results, true);
510 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
511 Assert.IsNull (results.PathToAssembly, "#2");
513 // verify we don't cleanup files in temp directory too agressively
514 string[] tempFiles = Directory.GetFiles (_tempDir);
515 Assert.AreEqual (1, tempFiles.Length, "#3");
516 Assert.AreEqual (tempFile, tempFiles[0], "#4");
520 public void CompileFromDomBatch_InMemory ()
522 // create a file in temp directory to ensure that compiler is not removing
523 // too much (temporary) files
524 string tempFile = Path.Combine (_tempDir, "file." + _codeProvider.FileExtension);
525 using (FileStream fs = File.Create (tempFile)) {
529 CompilerParameters options = new CompilerParameters ();
530 options.GenerateExecutable = false;
531 options.GenerateInMemory = true;
532 options.TempFiles = new TempFileCollection (_tempDir);
534 ICodeCompiler compiler = _codeProvider.CreateCompiler ();
535 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options,
536 new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
538 // verify compilation was successful
539 AssertCompileResults (results, true);
541 Assert.AreEqual (string.Empty, results.CompiledAssembly.Location, "#1");
542 Assert.IsNull (results.PathToAssembly, "#2");
544 // verify we don't cleanup files in temp directory too agressively
545 string[] tempFiles = Directory.GetFiles (_tempDir);
546 Assert.AreEqual (1, tempFiles.Length, "#3");
547 Assert.AreEqual (tempFile, tempFiles[0], "#4");
550 private static string CreateTempDirectory ()
552 // create a uniquely named zero-byte file
553 string tempFile = Path.GetTempFileName ();
554 // remove the temporary file
555 File.Delete (tempFile);
556 // create a directory named after the unique temporary file
557 Directory.CreateDirectory (tempFile);
558 // return the path to the temporary directory
562 private static void RemoveDirectory (string path)
565 if (Directory.Exists (path)) {
566 string[] directoryNames = Directory.GetDirectories (path);
567 foreach (string directoryName in directoryNames) {
568 RemoveDirectory (directoryName);
570 string[] fileNames = Directory.GetFiles (path);
571 foreach (string fileName in fileNames) {
572 File.Delete (fileName);
574 Directory.Delete (path, true);
576 } catch (Exception ex) {
577 throw new AssertionException ("Unable to cleanup '" + path + "'.", ex);
581 private static void AssertCompileResults (CompilerResults results, bool allowWarnings)
583 foreach (CompilerError compilerError in results.Errors) {
584 if (allowWarnings && compilerError.IsWarning) {
588 throw new Exception (compilerError.ToString ());
592 private static AppDomain CreateTestDomain ()
594 return AppDomain.CreateDomain ("CompileFromDom", AppDomain.CurrentDomain.Evidence,
595 AppDomain.CurrentDomain.SetupInformation);
598 private static CrossDomainTester CreateCrossDomainTester (AppDomain domain)
600 Type testerType = typeof (CrossDomainTester);
602 return (CrossDomainTester) domain.CreateInstanceAndUnwrap (
603 testerType.Assembly.FullName, testerType.FullName, false,
604 BindingFlags.Public | BindingFlags.Instance, null, new object[0],
605 CultureInfo.InvariantCulture, new object[0], domain.Evidence);
608 // do not use the Assert class as this will introduce failures if the
609 // nunit.framework assembly is not in the GAC
610 private class CrossDomainTester : MarshalByRefObject
612 public string CompileAssemblyFromDom (string tempDir)
614 CompilerParameters options = new CompilerParameters ();
615 options.GenerateExecutable = false;
616 options.GenerateInMemory = false;
617 options.TempFiles = new TempFileCollection (tempDir);
619 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
620 ICodeCompiler compiler = codeProvider.CreateCompiler ();
621 CompilerResults results = compiler.CompileAssemblyFromDom (options, new CodeCompileUnit ());
623 // verify compilation was successful
624 AssertCompileResults (results, true);
626 if (results.CompiledAssembly.Location.Length == 0)
627 throw new Exception ("Location should not be empty string");
628 if (results.PathToAssembly == null)
629 throw new Exception ("PathToAssembly should not be null");
631 return results.PathToAssembly;
634 public string CompileAssemblyFromDomBatch (string tempDir)
636 CompilerParameters options = new CompilerParameters ();
637 options.GenerateExecutable = false;
638 options.GenerateInMemory = false;
639 options.TempFiles = new TempFileCollection (tempDir);
641 CSharpCodeProvider codeProvider = new CSharpCodeProvider ();
642 ICodeCompiler compiler = codeProvider.CreateCompiler ();
643 CompilerResults results = compiler.CompileAssemblyFromDomBatch (options, new CodeCompileUnit[] { new CodeCompileUnit (), new CodeCompileUnit () });
645 // verify compilation was successful
646 AssertCompileResults (results, true);
648 if (results.CompiledAssembly.Location.Length == 0)
649 throw new Exception ("Location should not be empty string");
650 if (results.PathToAssembly == null)
651 throw new Exception ("PathToAssembly should not be null");
653 return results.PathToAssembly;