2 // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
12 using System.Globalization;
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
17 using System.Configuration.Assemblies;
19 using NUnit.Framework;
21 namespace MonoTests.System.Reflection.Emit
25 public class AssemblyBuilderTest
27 [AttributeUsage (AttributeTargets.Assembly)]
28 public sealed class FooAttribute : Attribute
30 public FooAttribute (string arg)
34 public FooAttribute ()
39 static int nameIndex = 0;
41 static AppDomain domain;
43 static AssemblyBuilder ab;
45 static ModuleBuilder mb;
47 string tempDir = Path.Combine (Path.GetTempPath (), "MonoTests.System.Reflection.Emit.AssemblyBuilderTest");
50 protected void SetUp () {
51 if (Directory.Exists (tempDir))
52 Directory.Delete (tempDir, true);
54 Directory.CreateDirectory (tempDir);
56 for (int i = 1; i < 3; ++i) {
57 string resFile = Path.Combine (tempDir, "res" + i + ".txt");
58 using (StreamWriter sw = new StreamWriter (resFile)) {
63 domain = Thread.GetDomain ();
65 mb = ab.DefineDynamicModule ("def_module");
69 protected void TearDown () {
70 if (Directory.Exists (tempDir))
71 Directory.Delete (tempDir, true);
74 private AssemblyName genAssemblyName () {
75 AssemblyName assemblyName = new AssemblyName();
76 assemblyName.Name = "MonoTests.System.Reflection.Emit.AssemblyBuilderTest" + (nameIndex ++);
80 private AssemblyBuilder genAssembly () {
81 return domain.DefineDynamicAssembly (genAssemblyName (),
82 AssemblyBuilderAccess.RunAndSave,
86 private MethodInfo genEntryFunction (AssemblyBuilder assembly) {
87 ModuleBuilder module = assembly.DefineDynamicModule("module1");
88 TypeBuilder tb = module.DefineType ("A");
89 MethodBuilder mb = tb.DefineMethod ("A",
90 MethodAttributes.Static, typeof (void), new Type [0]);
91 mb.GetILGenerator ().Emit (OpCodes.Ret);
95 [ExpectedException (typeof (NotSupportedException))]
96 public void TestCodeBase () {
97 string codebase = ab.CodeBase;
100 [ExpectedException (typeof (NotSupportedException))]
101 public void TestLocation () {
102 string location = ab.Location;
105 public void TestEntryPoint () {
106 Assert.AreEqual (null, ab.EntryPoint, "EntryPoint defaults to null");
108 MethodInfo mi = genEntryFunction (ab);
109 ab.SetEntryPoint (mi);
111 Assert.AreEqual (mi, ab.EntryPoint, "EntryPoint works");
114 public void TestSetEntryPoint () {
115 // Check invalid arguments
117 ab.SetEntryPoint (null);
120 catch (ArgumentNullException) {
123 // Check method from other assembly
125 ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
128 catch (InvalidOperationException) {
132 public void TestIsDefined () {
133 CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
134 ab.SetCustomAttribute (cab);
136 Assert.IsTrue (ab.IsDefined (typeof (FooAttribute), false),
137 "IsDefined(FooAttribute) works");
138 Assert.IsFalse (ab.IsDefined (typeof (AssemblyVersionAttribute), false),
139 "!IsDefined(AssemblyVersionAttribute) works");
142 [ExpectedException (typeof (NotSupportedException))]
143 public void TestGetManifestResourceNames () {
144 ab.GetManifestResourceNames ();
147 [ExpectedException (typeof (NotSupportedException))]
148 public void TestGetManifestResourceInfo () {
149 ab.GetManifestResourceInfo ("foo");
152 [ExpectedException (typeof (NotSupportedException))]
153 public void TestGetManifestResourceStream1 () {
154 ab.GetManifestResourceStream ("foo");
157 [ExpectedException (typeof (NotSupportedException))]
158 public void TestGetManifestResourceStream2 () {
159 ab.GetManifestResourceStream (typeof (int), "foo");
162 [ExpectedException (typeof (NotSupportedException))]
163 public void TestGetFiles1 () {
167 [ExpectedException (typeof (NotSupportedException))]
168 public void TestGetFiles2 () {
172 [ExpectedException (typeof (NotSupportedException))]
173 public void TestGetFile () {
177 [ExpectedException (typeof (NotSupportedException))]
178 public void TestGetExportedTypes () {
179 ab.GetExportedTypes ();
182 [ExpectedException (typeof (ArgumentNullException))]
183 public void TestGetDynamicModule1 () {
184 ab.GetDynamicModule (null);
187 [ExpectedException (typeof (ArgumentException))]
188 public void TestGetDynamicModule2 () {
189 ab.GetDynamicModule ("");
192 public void TestGetDynamicModule3 () {
193 Assert.IsNull (ab.GetDynamicModule ("FOO2"));
195 ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
197 Assert.AreEqual (mb, ab.GetDynamicModule ("FOO"));
199 Assert.IsNull (ab.GetDynamicModule ("FOO4"));
203 public void TestImageRuntimeVersion () {
204 string version = ab.ImageRuntimeVersion;
205 Assert.IsTrue (version.Length > 0);
209 [ExpectedException (typeof (ArgumentNullException))]
210 public void TestAddResourceFileNullName () {
211 ab.AddResourceFile (null, "foo.txt");
214 [ExpectedException (typeof (ArgumentNullException))]
215 public void TestAddResourceFileNullFilename () {
216 ab.AddResourceFile ("foo", null);
219 [ExpectedException (typeof (ArgumentException))]
220 public void TestAddResourceFileEmptyName () {
221 ab.AddResourceFile ("", "foo.txt");
224 [ExpectedException (typeof (ArgumentException))]
225 public void TestAddResourceFileEmptyFilename () {
226 ab.AddResourceFile ("foo", "");
229 [ExpectedException (typeof (FileNotFoundException))]
230 public void TestAddResourceFileNonexistentFile () {
231 ab.AddResourceFile ("foo", "not-existent.txt");
234 [ExpectedException (typeof (ArgumentException))]
235 public void TestAddResourceFileDuplicateFileName () {
236 ab.AddResourceFile ("foo", "res1.txt");
237 ab.AddResourceFile ("foo2", "res1.txt");
240 [ExpectedException (typeof (ArgumentException))]
241 public void TestAddResourceFileDuplicateName () {
242 ab.AddResourceFile ("foo", "res1.txt");
243 ab.AddResourceFile ("foo", "res2.txt");
246 [ExpectedException (typeof (ArgumentException))]
247 public void TestAddResourceFileFilenameIncludesPath () {
248 ab.AddResourceFile ("foo", "/tmp/res1.txt");
251 public void TestAddResourceFile () {
252 ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);
254 ab.Save ("TestAddResourceFile.dll");
256 // TODO: Test reading back
259 public void TestDefineResource () {
260 ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);
261 ab.DefineResource ("foo2", "FOO", "foo2.txt");
263 ab.Save ("TestDefineResource.dll");
266 [ExpectedException (typeof (ArgumentNullException))]
267 public void TestDefineDynamicModuleNullName () {
268 ab.DefineDynamicModule (null, "foo.txt");
271 [ExpectedException (typeof (ArgumentNullException))]
272 public void TestDefineDynamicModuleNullFilename () {
273 ab.DefineDynamicModule ("foo", null);
276 [ExpectedException (typeof (ArgumentException))]
277 public void TestDefineDynamicModuleEmptyName () {
278 ab.DefineDynamicModule ("", "foo.txt");
281 [ExpectedException (typeof (ArgumentException))]
282 public void TestDefineDynamicModuleEmptyFilename () {
283 ab.DefineDynamicModule ("foo", "");
286 [ExpectedException (typeof (ArgumentException))]
287 public void TestDefineDynamicModuleDuplicateFileName () {
288 ab.DefineDynamicModule ("foo", "res1.txt");
289 ab.DefineDynamicModule ("foo2", "res1.txt");
292 [ExpectedException (typeof (ArgumentException))]
293 public void TestDefineDynamicModuleDuplicateName () {
294 ab.DefineDynamicModule ("foo", "res1.txt");
295 ab.DefineDynamicModule ("foo", "res2.txt");
298 [ExpectedException (typeof (ArgumentException))]
299 public void TestDefineDynamicModuleFilenameIncludesPath () {
300 ab.DefineDynamicModule ("foo", "/tmp/res1.txt");
303 [ExpectedException (typeof (ArgumentException))]
304 public void TestDefineDynamicModule5 () {
305 // Filename without extension
306 ab.DefineDynamicModule ("foo", "foo");
310 [ExpectedException (typeof (ArgumentException))]
311 public void TestDefineDynamicModule6 () {
314 for (int i = 0; i < 259; ++i)
318 ab.DefineDynamicModule (name);
325 // LAMESPEC: According to MSDN, this should throw an ArgumentException
327 ab.DefineDynamicModule (name);
331 [ExpectedException (typeof (InvalidOperationException))]
332 public void TestDefineDynamicModule7 () {
333 // Called when assembly was already saved
334 ab.Save ("TestDefineDynamicModule7.dll");
335 ab.DefineDynamicModule ("foo", "foo.dll");
338 [ExpectedException (typeof (NotSupportedException))]
339 public void TestDefineDynamicModule8 () {
340 // Called on an assembly defined with the Run attribute
342 domain.DefineDynamicAssembly (genAssemblyName (),
343 AssemblyBuilderAccess.Run,
345 ab.DefineDynamicModule ("foo", "foo.dll");
348 public void TestDefineDynamicModule () {
349 ab.DefineDynamicModule ("foo", "foo.dll");
350 ab.DefineDynamicModule ("foo2", true);
351 ab.DefineDynamicModule ("foo3", "foo3.dll");
352 ab.DefineDynamicModule ("foo4", "foo4.dll", true);
355 [ExpectedException (typeof (ArgumentNullException))]
356 public void TestDefineUnmanagedResource1 () {
358 ab.DefineUnmanagedResource ((byte[])null);
361 [ExpectedException (typeof (ArgumentNullException))]
362 public void TestDefineUnmanagedResource2 () {
364 ab.DefineUnmanagedResource ((string)null);
367 [ExpectedException (typeof (ArgumentException))]
368 public void TestDefineUnmanagedResource3 () {
370 ab.DefineUnmanagedResource ("");
373 [ExpectedException (typeof (FileNotFoundException))]
374 public void TestDefineUnmanagedResource4 () {
376 ab.DefineUnmanagedResource ("not-exists.txt");
379 [ExpectedException (typeof (ArgumentNullException))]
380 public void TestSetCustomAttribute1 () {
382 ab.SetCustomAttribute (null);
385 [ExpectedException (typeof (ArgumentNullException))]
386 public void TestSetCustomAttribute2 () {
388 ab.SetCustomAttribute (null, new byte [0]);
391 [ExpectedException (typeof (ArgumentNullException))]
392 public void TestSetCustomAttribute3 () {
394 ab.SetCustomAttribute (typeof(AssemblyCompanyAttribute).GetConstructor (new Type [] { typeof (String) }), null);
397 public void TestSetCustomAttribute () {
398 // Test common custom attributes
400 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));
402 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));
404 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }), new object [] { AssemblyHashAlgorithm.MD5 }));
406 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0x0100 }));
408 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
410 ab.SetCustomAttribute (typeof (FooAttribute).GetConstructor (new Type [] {}), new byte [0]);
412 ab.Save ("TestSetCustomAttribute.dll");
414 /* We should read back the assembly and check the attributes ... */
417 // strongname generated using "sn -k unit.snk"
418 static byte[] strongName = {
419 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
420 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A,
421 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D,
422 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72,
423 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16,
424 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4,
425 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4,
426 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D,
427 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25,
428 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93,
429 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0,
430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 0xBA, 0xF3, 0xA6, 0x65, 0x7A,
460 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27,
461 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54,
462 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57,
463 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1,
464 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96,
465 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE,
466 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9,
467 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F,
468 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
471 public void StrongName_MissingKeyFile_NoDelay ()
473 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
474 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
475 ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
477 string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
478 AssemblyName check = AssemblyName.GetAssemblyName (filename);
479 // no exception is thrown (file not found)
480 // because it's not AssemblyBuilder.Save job to do the signing :-/
481 Assert.IsNull (check.GetPublicKeyToken (), "Token");
485 public void StrongName_KeyFile_Delay ()
487 string strongfile = Path.Combine (tempDir, "strongname.snk");
488 using (FileStream fs = File.OpenWrite (strongfile)) {
489 fs.Write (strongName, 0, strongName.Length);
492 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
493 ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
494 ab.Save ("StrongName_KeyFile_Delay.dll");
496 string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
497 AssemblyName check = AssemblyName.GetAssemblyName (filename);
498 // no public key is inserted into the assembly
499 // because it's not AssemblyBuilder.Save job to do the signing :-/
500 Assert.IsNull (check.GetPublicKeyToken (), "Token");
504 public void StrongName_WithoutAttributes ()
506 // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
507 // aren't required to sign an assembly.
508 AssemblyName an = genAssemblyName ();
509 an.KeyPair = new StrongNameKeyPair (strongName);
510 AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
511 ab.Save ("StrongName_WithoutAttributes.dll");
513 string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
514 AssemblyName check = AssemblyName.GetAssemblyName (filename);
515 Assert.AreEqual ("0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()), "Token");
519 [ExpectedException (typeof (NotSupportedException))]
520 public void SaveUnfinishedTypes ()
522 TypeBuilder typeBuilder = mb.DefineType ("TestType", TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.AnsiClass | TypeAttributes.AutoClass, typeof(object));
524 ab.Save ("def_module");
528 public void GetModules ()
532 m = ab.GetModules ();
533 Assert.IsTrue (m.Length >= 2);
535 // Test with no modules
536 AssemblyBuilder ab2 = genAssembly ();
537 m = ab2.GetModules ();
541 [Category ("NotWorking")]
542 public void AssemblyName_Culture ()
544 AssemblyName assemblyName = new AssemblyName ();
545 assemblyName.Name = "AssemblyNameTest";
546 assemblyName.Version = new Version ("1.0.0.0");
547 assemblyName.CultureInfo = new CultureInfo ("en-US");
549 const string fullName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null";
550 const string abName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US";
552 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
556 public void AssemblyName_PublicKey ()
558 AssemblyName assemblyName = new AssemblyName ();
559 assemblyName.Name = "AssemblyNameTest_PublicKey";
560 assemblyName.Version = new Version ("1.2.3.4");
561 assemblyName.KeyPair = new StrongNameKeyPair (strongName);
563 Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4", assemblyName.FullName, "#1");
565 const string fullName = "AssemblyNameTest_PublicKey, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
567 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
568 assemblyName, AssemblyBuilderAccess.Save, tempDir);
570 AssemblyName abName = ab.GetName ();
571 Assert.IsNotNull (abName.GetPublicKeyToken (), "#2");
572 Assert.IsTrue (abName.GetPublicKeyToken ().Length > 0, "#3");
573 Assert.IsNotNull (abName.GetPublicKey () != null, "#4");
574 Assert.IsTrue (abName.GetPublicKey ().Length > 0, "#5");
576 ab.Save ("AssemblyNameTest_PublicKey.dll");
577 AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
578 tempDir, "AssemblyNameTest_PublicKey.dll"));
580 Assert.IsNotNull (bakedName.GetPublicKeyToken (), "#6");
581 Assert.IsNotNull (bakedName.GetPublicKey (), "#7");
582 Assert.AreEqual (fullName, bakedName.FullName, "#8");
586 [Category ("NotWorking")]
587 public void AssemblyName_MoreCultureInfo ()
589 AssemblyName assemblyName = new AssemblyName ();
590 assemblyName.Name = "AssemblyNameTest_MoreCultureInfo";
591 assemblyName.Version = new Version ("1.2.3.4");
592 assemblyName.KeyPair = new StrongNameKeyPair (strongName);
594 Assert.IsNull (assemblyName.CultureInfo, "#1");
595 Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4", assemblyName.FullName, "#2");
597 const string fullName = "AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
599 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
600 assemblyName, AssemblyBuilderAccess.Save, tempDir);
602 AssemblyName abName = ab.GetName ();
603 Assert.IsNotNull (abName.CultureInfo != null, "#3");
605 Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#4");
606 Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#5");
607 Assert.AreEqual (fullName, abName.FullName, "#6");
609 Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#7");
610 Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, PublicKeyToken=0eea7ce65f35f2d8", abName.FullName, "#8");
613 ab.Save ("AssemblyNameTest_MoreCultureInfo.dll");
615 AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
616 tempDir, "AssemblyNameTest_MoreCultureInfo.dll"));
618 Assert.IsNotNull (bakedName.CultureInfo, "#9");
621 Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#10");
622 Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#11");
624 Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#12");
626 Assert.AreEqual (fullName, bakedName.FullName, "#13");
630 [Category ("NotWorking")]
631 public void AssemblyName_NoVersion ()
633 AssemblyName assemblyName = new AssemblyName ();
634 assemblyName.Name = "AssemblyNameTest";
636 const string fullName = "AssemblyNameTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
637 const string abName = "AssemblyNameTest, Version=0.0.0.0";
639 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
643 [Category ("NotWorking")]
644 public void AssemblyName_Version ()
646 AssemblyName assemblyName = new AssemblyName ();
647 assemblyName.Name = "AssemblyNameTest";
648 assemblyName.Version = new Version (1, 2, 3, 4);
650 const string fullName = "AssemblyNameTest, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null";
651 const string abName = "AssemblyNameTest, Version=1.2.3.4";
653 AssertAssemblyName (tempDir, assemblyName, abName, fullName);
657 public void GetType_IgnoreCase ()
659 TypeBuilder tb = mb.DefineType ("Foo.Test2", TypeAttributes.Public, typeof (object));
663 t = ab.GetType ("foo.Test2", true, true);
664 Assert.AreEqual ("Test2", t.Name);
666 t = ab.GetType ("foo.test2", true, true);
667 Assert.AreEqual ("Test2", t.Name);
669 t = ab.GetType ("Foo.test2", true, true);
670 Assert.AreEqual ("Test2", t.Name);
673 private static void AssertAssemblyName (string tempDir, AssemblyName assemblyName, string abName, string fullName)
675 AppDomain currentDomain = AppDomain.CurrentDomain;
676 AppDomain newDomain = null;
679 AssemblyBuilder ab = currentDomain.DefineDynamicAssembly (
680 assemblyName, AssemblyBuilderAccess.Save, tempDir);
681 ab.Save (assemblyName.Name + ".dll");
684 // on .NET 2.0, the full name of the AssemblyBuilder matches the
685 // fully qualified assembly name
686 Assert.AreEqual (fullName, ab.FullName);
688 Assert.AreEqual (abName, ab.FullName);
691 // load assembly in separate domain, so we can clean-up after the
693 newDomain = AppDomain.CreateDomain ("test2", currentDomain.Evidence,
694 currentDomain.SetupInformation);
696 Helper helper = new Helper (Path.Combine (tempDir, assemblyName.Name + ".dll"),
698 newDomain.DoCallBack (new CrossAppDomainDelegate (helper.Test));
700 if (newDomain != null) {
701 AppDomain.Unload (newDomain);
709 private readonly string _assemblyPath;
710 private readonly string _assemblyName;
712 public Helper (string assemblyPath, string assemblyName)
714 _assemblyPath = assemblyPath;
715 _assemblyName = assemblyName;
719 AssemblyName assemblyName = AssemblyName.GetAssemblyName (_assemblyPath);
720 Assert.AreEqual (_assemblyName, assemblyName.ToString ());