AssemblyBuilderTest.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. //
  2. // AssemblyBuilderTest.cs - NUnit Test Cases for the AssemblyBuilder class
  3. //
  4. // Zoltan Varga ([email protected])
  5. // Andres G. Aragoneses ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. // (C) 7digital Media, Ltd. http://www.7digital.com
  9. //
  10. //
  11. using System;
  12. using System.Globalization;
  13. using System.Threading;
  14. using System.Reflection;
  15. using System.Reflection.Emit;
  16. using System.IO;
  17. using System.Configuration.Assemblies;
  18. using NUnit.Framework;
  19. namespace MonoTests.System.Reflection.Emit
  20. {
  21. [TestFixture]
  22. public class AssemblyBuilderTest
  23. {
  24. [AttributeUsage (AttributeTargets.Assembly)]
  25. public sealed class FooAttribute : Attribute
  26. {
  27. public FooAttribute (string arg)
  28. {
  29. }
  30. public FooAttribute ()
  31. {
  32. }
  33. }
  34. static int nameIndex = 0;
  35. static AppDomain domain;
  36. static AssemblyBuilder ab;
  37. static ModuleBuilder mb;
  38. string tempDir = Path.Combine (Path.GetTempPath (), typeof (AssemblyBuilderTest).FullName);
  39. [SetUp]
  40. protected void SetUp ()
  41. {
  42. if (Directory.Exists (tempDir))
  43. Directory.Delete (tempDir, true);
  44. Directory.CreateDirectory (tempDir);
  45. for (int i = 1; i < 3; ++i) {
  46. string resFile = Path.Combine (tempDir, "res" + i + ".txt");
  47. using (StreamWriter sw = new StreamWriter (resFile)) {
  48. sw.WriteLine ("FOO");
  49. }
  50. }
  51. domain = Thread.GetDomain ();
  52. ab = genAssembly ();
  53. mb = ab.DefineDynamicModule ("def_module");
  54. }
  55. [TearDown]
  56. protected void TearDown ()
  57. {
  58. if (Directory.Exists (tempDir))
  59. Directory.Delete (tempDir, true);
  60. }
  61. private AssemblyName genAssemblyName ()
  62. {
  63. AssemblyName assemblyName = new AssemblyName();
  64. assemblyName.Name = typeof (AssemblyBuilderTest).FullName + (nameIndex ++);
  65. return assemblyName;
  66. }
  67. private AssemblyBuilder genAssembly (AssemblyBuilderAccess access)
  68. {
  69. return domain.DefineDynamicAssembly (genAssemblyName (),
  70. access,
  71. tempDir);
  72. }
  73. private AssemblyBuilder genAssembly ()
  74. {
  75. return genAssembly (AssemblyBuilderAccess.RunAndSave);
  76. }
  77. private MethodInfo genEntryFunction (AssemblyBuilder assembly)
  78. {
  79. ModuleBuilder module = assembly.DefineDynamicModule("module1");
  80. TypeBuilder tb = module.DefineType ("A");
  81. MethodBuilder mb = tb.DefineMethod ("A",
  82. MethodAttributes.Static, typeof (void), new Type [0]);
  83. mb.GetILGenerator ().Emit (OpCodes.Ret);
  84. return mb;
  85. }
  86. [Test]
  87. [Category ("NotWorking")]
  88. public void ManifestModule ()
  89. {
  90. AssemblyName aname = new AssemblyName ("ManifestModule1");
  91. ab = domain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.RunAndSave,
  92. tempDir);
  93. Assert.IsNotNull (ab.ManifestModule, "#A1");
  94. Assert.AreEqual (1, ab.GetModules ().Length, "#A2");
  95. Assert.AreEqual (typeof (ModuleBuilder), ab.ManifestModule.GetType (), "#A3");
  96. ModuleBuilder mb1 = (ModuleBuilder) ab.ManifestModule;
  97. Assert.AreSame (mb1, ab.GetModules () [0], "#B1");
  98. Assert.IsFalse (mb1.IsResource (), "#B2");
  99. Assert.AreSame (mb1, ab.ManifestModule, "#B3");
  100. ab.Save ("ManifestModule.dll");
  101. ModuleBuilder mb2 = (ModuleBuilder) ab.ManifestModule;
  102. Assert.AreSame (mb2, ab.GetModules () [0], "#C1");
  103. Assert.IsFalse (mb2.IsResource (), "#C2");
  104. Assert.AreSame (mb2, ab.ManifestModule, "#C3");
  105. Assert.AreSame (mb1, mb2, "#C4");
  106. }
  107. [Test]
  108. [ExpectedException (typeof (NotSupportedException))]
  109. public void TestCodeBase ()
  110. {
  111. string codebase = ab.CodeBase;
  112. }
  113. [Test]
  114. [ExpectedException (typeof (NotSupportedException))]
  115. public void TestLocation ()
  116. {
  117. string location = ab.Location;
  118. }
  119. [Test]
  120. public void TestEntryPoint ()
  121. {
  122. Assert.AreEqual (null, ab.EntryPoint, "EntryPoint defaults to null");
  123. MethodInfo mi = genEntryFunction (ab);
  124. ab.SetEntryPoint (mi);
  125. Assert.AreEqual (mi, ab.EntryPoint, "EntryPoint works");
  126. }
  127. [Test]
  128. public void TestSetEntryPoint ()
  129. {
  130. // Check invalid arguments
  131. try {
  132. ab.SetEntryPoint (null);
  133. Assert.Fail ("#A1");
  134. } catch (ArgumentNullException ex) {
  135. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
  136. Assert.IsNull (ex.InnerException, "#A3");
  137. Assert.IsNotNull (ex.Message, "#A4");
  138. Assert.IsNotNull (ex.ParamName, "#A5");
  139. Assert.AreEqual ("entryMethod", ex.ParamName, "#A6");
  140. }
  141. // Check method from other assembly
  142. try {
  143. ab.SetEntryPoint (typeof (AssemblyBuilderTest).GetMethod ("TestSetEntryPoint"));
  144. Assert.Fail ("#B");
  145. } catch (InvalidOperationException) {
  146. }
  147. }
  148. [Test]
  149. public void TestIsDefined ()
  150. {
  151. CustomAttributeBuilder cab = new CustomAttributeBuilder (typeof (FooAttribute).GetConstructor (new Type [1] {typeof (string)}), new object [1] { "A" });
  152. ab.SetCustomAttribute (cab);
  153. Assert.IsTrue (ab.IsDefined (typeof (FooAttribute), false),
  154. "IsDefined(FooAttribute) works");
  155. Assert.IsFalse (ab.IsDefined (typeof (AssemblyVersionAttribute), false),
  156. "!IsDefined(AssemblyVersionAttribute) works");
  157. }
  158. [Test]
  159. [ExpectedException (typeof (NotSupportedException))]
  160. public void TestGetManifestResourceNames ()
  161. {
  162. ab.GetManifestResourceNames ();
  163. }
  164. [Test]
  165. [ExpectedException (typeof (NotSupportedException))]
  166. public void TestGetManifestResourceInfo ()
  167. {
  168. ab.GetManifestResourceInfo ("foo");
  169. }
  170. [Test]
  171. [ExpectedException (typeof (NotSupportedException))]
  172. public void TestGetManifestResourceStream1 ()
  173. {
  174. ab.GetManifestResourceStream ("foo");
  175. }
  176. [Test]
  177. [ExpectedException (typeof (NotSupportedException))]
  178. public void TestGetManifestResourceStream2 ()
  179. {
  180. ab.GetManifestResourceStream (typeof (int), "foo");
  181. }
  182. [Test]
  183. [ExpectedException (typeof (NotSupportedException))]
  184. public void TestGetFiles1 ()
  185. {
  186. ab.GetFiles ();
  187. }
  188. [Test]
  189. [ExpectedException (typeof (NotSupportedException))]
  190. public void TestGetFiles2 ()
  191. {
  192. ab.GetFiles (true);
  193. }
  194. [Test]
  195. [ExpectedException (typeof (NotSupportedException))]
  196. public void TestGetFile ()
  197. {
  198. ab.GetFile ("foo");
  199. }
  200. [Test]
  201. [ExpectedException (typeof (NotSupportedException))]
  202. public void TestGetExportedTypes ()
  203. {
  204. ab.GetExportedTypes ();
  205. }
  206. [Test]
  207. public void TestGetDynamicModule_Name_Null ()
  208. {
  209. try {
  210. ab.GetDynamicModule (null);
  211. Assert.Fail ("#1");
  212. } catch (ArgumentNullException ex) {
  213. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  214. Assert.IsNull (ex.InnerException, "#3");
  215. Assert.IsNotNull (ex.Message, "#4");
  216. Assert.IsNotNull (ex.ParamName, "#5");
  217. Assert.AreEqual ("name", ex.ParamName, "#6");
  218. }
  219. }
  220. [Test]
  221. public void TestGetDynamicModule_Name_Empty ()
  222. {
  223. try {
  224. ab.GetDynamicModule (string.Empty);
  225. Assert.Fail ("#1");
  226. } catch (ArgumentException ex) {
  227. // Empty name is not legal
  228. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  229. Assert.IsNull (ex.InnerException, "#3");
  230. Assert.IsNotNull (ex.Message, "#4");
  231. Assert.IsNotNull (ex.ParamName, "#5");
  232. Assert.AreEqual ("name", ex.ParamName, "#6");
  233. }
  234. }
  235. [Test]
  236. public void TestGetDynamicModule3 ()
  237. {
  238. Assert.IsNull (ab.GetDynamicModule ("FOO2"));
  239. ModuleBuilder mb = ab.DefineDynamicModule ("FOO");
  240. Assert.AreEqual (mb, ab.GetDynamicModule ("FOO"));
  241. Assert.IsNull (ab.GetDynamicModule ("FOO4"));
  242. }
  243. [Test]
  244. public void TestImageRuntimeVersion ()
  245. {
  246. string version = ab.ImageRuntimeVersion;
  247. Assert.IsTrue (version.Length > 0);
  248. }
  249. [Test]
  250. public void TestAddResourceFile_Name_Null ()
  251. {
  252. try {
  253. ab.AddResourceFile (null, "foo.txt");
  254. Assert.Fail ("#1");
  255. } catch (ArgumentNullException ex) {
  256. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  257. Assert.IsNull (ex.InnerException, "#3");
  258. Assert.IsNotNull (ex.Message, "#4");
  259. Assert.IsNotNull (ex.ParamName, "#5");
  260. Assert.AreEqual ("name", ex.ParamName, "#6");
  261. }
  262. }
  263. [Test]
  264. public void TestAddResourceFile_Filename_Null ()
  265. {
  266. try {
  267. ab.AddResourceFile ("foo", null);
  268. Assert.Fail ("#1");
  269. } catch (ArgumentNullException ex) {
  270. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  271. Assert.IsNull (ex.InnerException, "#3");
  272. Assert.IsNotNull (ex.Message, "#4");
  273. Assert.IsNotNull (ex.ParamName, "#5");
  274. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  275. }
  276. }
  277. [Test]
  278. public void TestAddResourceFile_Name_Empty ()
  279. {
  280. try {
  281. ab.AddResourceFile (string.Empty, "foo.txt");
  282. Assert.Fail ("#1");
  283. } catch (ArgumentException ex) {
  284. // Empty name is not legal
  285. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  286. Assert.IsNull (ex.InnerException, "#3");
  287. Assert.IsNotNull (ex.Message, "#4");
  288. }
  289. }
  290. [Test]
  291. public void TestAddResourceFile_Filename_Empty ()
  292. {
  293. try {
  294. ab.AddResourceFile ("foo", string.Empty);
  295. Assert.Fail ("#1");
  296. } catch (ArgumentException ex) {
  297. // Empty file name is not legal
  298. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  299. Assert.IsNull (ex.InnerException, "#3");
  300. Assert.IsNotNull (ex.Message, "#4");
  301. }
  302. }
  303. [Test]
  304. [ExpectedException (typeof (FileNotFoundException))]
  305. public void TestAddResourceFile_FileName_DoesNotExist ()
  306. {
  307. ab.AddResourceFile ("foo", "not-existent.txt");
  308. }
  309. [Test]
  310. public void TestAddResourceFile_FileName_Duplicate ()
  311. {
  312. ab.AddResourceFile ("foo", "res1.txt");
  313. try {
  314. ab.AddResourceFile ("foo2", "res1.txt");
  315. Assert.Fail ("#1");
  316. } catch (ArgumentException ex) {
  317. // Duplicate file names
  318. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  319. Assert.IsNull (ex.InnerException, "#3");
  320. Assert.IsNotNull (ex.Message, "#4");
  321. Assert.IsNull (ex.ParamName, "#5");
  322. }
  323. }
  324. [Test]
  325. public void TestAddResourceFile_Name_Duplicate ()
  326. {
  327. ab.AddResourceFile ("foo", "res1.txt");
  328. try {
  329. ab.AddResourceFile ("foo", "res2.txt");
  330. Assert.Fail ("#1");
  331. } catch (ArgumentException ex) {
  332. // Duplicate resource name within an assembly
  333. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  334. Assert.IsNull (ex.InnerException, "#3");
  335. Assert.IsNotNull (ex.Message, "#4");
  336. Assert.IsNull (ex.ParamName, "#5");
  337. }
  338. }
  339. [Test]
  340. public void TestAddResourceFile_Filename_IncludesPath ()
  341. {
  342. try {
  343. ab.AddResourceFile ("foo", "/tmp/res1.txt");
  344. Assert.Fail ("#1");
  345. } catch (ArgumentException ex) {
  346. // The filename must not include a path specification
  347. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  348. Assert.IsNull (ex.InnerException, "#3");
  349. Assert.IsNotNull (ex.Message, "#4");
  350. Assert.IsNotNull (ex.ParamName, "#5");
  351. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  352. }
  353. }
  354. [Test]
  355. public void TestAddResourceFile ()
  356. {
  357. ab.AddResourceFile ("foo", "res2.txt", ResourceAttributes.Public);
  358. ab.Save ("TestAddResourceFile.dll");
  359. // TODO: Test reading back
  360. }
  361. [Test]
  362. [Category ("AndroidNotWorking")] // DefineResource doesn't allow path in its fileName parameter and the test attempts to write to / in effect
  363. public void TestDefineResource ()
  364. {
  365. ab.DefineResource ("foo", "FOO", "foo.txt", ResourceAttributes.Public);
  366. ab.DefineResource ("foo2", "FOO", "foo2.txt");
  367. ab.Save ("TestDefineResource.dll");
  368. }
  369. [Test]
  370. public void TestDefineDynamicModule_Name_Null ()
  371. {
  372. try {
  373. ab.DefineDynamicModule (null, "foo.txt");
  374. Assert.Fail ("#1");
  375. } catch (ArgumentNullException ex) {
  376. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  377. Assert.IsNull (ex.InnerException, "#3");
  378. Assert.IsNotNull (ex.Message, "#4");
  379. Assert.IsNotNull (ex.ParamName, "#5");
  380. Assert.AreEqual ("name", ex.ParamName, "#6");
  381. }
  382. }
  383. [Test]
  384. public void TestDefineDynamicModule_FileName_Null ()
  385. {
  386. try {
  387. ab.DefineDynamicModule ("foo", null);
  388. Assert.Fail ("#1");
  389. } catch (ArgumentNullException ex) {
  390. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  391. Assert.IsNull (ex.InnerException, "#3");
  392. Assert.IsNotNull (ex.Message, "#4");
  393. Assert.IsNotNull (ex.ParamName, "#5");
  394. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  395. }
  396. }
  397. [Test]
  398. public void TestDefineDynamicModule_Name_Empty ()
  399. {
  400. try {
  401. ab.DefineDynamicModule (string.Empty, "foo.txt");
  402. Assert.Fail ("#1");
  403. } catch (ArgumentException ex) {
  404. // Empty name is not legal
  405. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  406. Assert.IsNull (ex.InnerException, "#3");
  407. Assert.IsNotNull (ex.Message, "#4");
  408. Assert.IsNotNull (ex.ParamName, "#5");
  409. Assert.AreEqual ("name", ex.ParamName, "#6");
  410. }
  411. }
  412. [Test]
  413. public void TestDefineDynamicModule_Filename_Empty ()
  414. {
  415. try {
  416. ab.DefineDynamicModule ("foo", string.Empty);
  417. Assert.Fail ("#1");
  418. } catch (ArgumentException ex) {
  419. // Empty file name is not legal
  420. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  421. Assert.IsNull (ex.InnerException, "#3");
  422. Assert.IsNotNull (ex.Message, "#4");
  423. Assert.IsNotNull (ex.ParamName, "#5");
  424. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  425. }
  426. }
  427. [Test]
  428. public void TestDefineDynamicModule_FileName_Duplicate ()
  429. {
  430. ab.DefineDynamicModule ("foo", "res1.txt");
  431. try {
  432. ab.DefineDynamicModule ("foo2", "res1.txt");
  433. Assert.Fail ("#1");
  434. } catch (ArgumentException ex) {
  435. // Duplicate file names
  436. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  437. Assert.IsNull (ex.InnerException, "#3");
  438. Assert.IsNotNull (ex.Message, "#4");
  439. Assert.IsNull (ex.ParamName, "#5");
  440. }
  441. }
  442. [Test]
  443. public void TestDefineDynamicModule_Name_Duplicate ()
  444. {
  445. ab.DefineDynamicModule ("foo", "res1.txt");
  446. try {
  447. ab.DefineDynamicModule ("foo", "res2.txt");
  448. Assert.Fail ("#1");
  449. } catch (ArgumentException ex) {
  450. // Duplicate dynamic module name within an assembly
  451. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  452. Assert.IsNull (ex.InnerException, "#3");
  453. Assert.IsNotNull (ex.Message, "#4");
  454. Assert.IsNull (ex.ParamName, "#5");
  455. }
  456. }
  457. [Test]
  458. public void TestDefineDynamicModule_Filename_IncludesPath ()
  459. {
  460. try {
  461. ab.DefineDynamicModule ("foo", "/tmp/res1.txt");
  462. Assert.Fail ("#1");
  463. } catch (ArgumentException ex) {
  464. // The filename must not include a path specification
  465. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  466. Assert.IsNull (ex.InnerException, "#3");
  467. Assert.IsNotNull (ex.Message, "#4");
  468. Assert.IsNotNull (ex.ParamName, "#5");
  469. Assert.AreEqual ("fileName", ex.ParamName, "#6");
  470. }
  471. }
  472. [Test]
  473. public void TestDefineDynamicModule5_FileName_NoExtension ()
  474. {
  475. try {
  476. ab.DefineDynamicModule ("foo", "bar");
  477. Assert.Fail ("#1");
  478. } catch (ArgumentException ex) {
  479. // Module file name 'bar' must have file extension
  480. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  481. Assert.IsNull (ex.InnerException, "#3");
  482. Assert.IsNotNull (ex.Message, "#4");
  483. Assert.IsTrue (ex.Message.IndexOf ("bar") != -1, "#5");
  484. Assert.IsNull (ex.ParamName, "#6");
  485. }
  486. }
  487. [Test]
  488. [Category ("NotWorking")]
  489. public void TestDefineDynamicModule_Name_MaxLength () {
  490. string name = string.Empty;
  491. for (int i = 0; i < 259; ++i)
  492. name = name + "A";
  493. ab.DefineDynamicModule (name);
  494. name = name + "A";
  495. try {
  496. ab.DefineDynamicModule (name);
  497. Assert.Fail ("#1");
  498. } catch (ArgumentException ex) {
  499. // Value does not fall within expected range
  500. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  501. Assert.IsNull (ex.InnerException, "#3");
  502. Assert.IsNotNull (ex.Message, "#4");
  503. Assert.IsNull (ex.ParamName, "#5");
  504. }
  505. }
  506. [Test]
  507. [ExpectedException (typeof (InvalidOperationException))]
  508. public void TestDefineDynamicModule_Assembly_Saved ()
  509. {
  510. // Called when assembly was already saved
  511. ab.Save ("TestDefineDynamicModule7.dll");
  512. ab.DefineDynamicModule ("foo", "foo.dll");
  513. }
  514. [Test]
  515. [ExpectedException (typeof (NotSupportedException))]
  516. public void TestDefineDynamicModule_Access_Run ()
  517. {
  518. // Called on an assembly defined with the Run attribute
  519. AssemblyBuilder ab =
  520. domain.DefineDynamicAssembly (genAssemblyName (),
  521. AssemblyBuilderAccess.Run,
  522. tempDir);
  523. ab.DefineDynamicModule ("foo", "foo.dll");
  524. }
  525. [Test]
  526. [Category ("AndroidNotWorking")] // Missing Mono.Compilerservices.SymbolWriter assembly
  527. public void TestDefineDynamicModule ()
  528. {
  529. ab.DefineDynamicModule ("foo", "foo.dll");
  530. ab.DefineDynamicModule ("foo2", true);
  531. ab.DefineDynamicModule ("foo3", "foo3.dll");
  532. ab.DefineDynamicModule ("foo4", "foo4.dll", true);
  533. }
  534. [Test] // DefineUnmanagedResource (byte [])
  535. [Category ("NotWorking")]
  536. public void TestDefineUnmanagedResource1_ResourceAlreadyDefined ()
  537. {
  538. string version_res = Path.Combine (tempDir, "version.res");
  539. using (FileStream fs = File.OpenWrite (version_res)) {
  540. fs.WriteByte (0x0a);
  541. }
  542. ab.DefineUnmanagedResource (new byte [0]);
  543. try {
  544. ab.DefineUnmanagedResource (new byte [0]);
  545. Assert.Fail ("#A1");
  546. } catch (ArgumentException ex) {
  547. // Native resource has already been defined
  548. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  549. Assert.IsNull (ex.InnerException, "#A3");
  550. Assert.IsNotNull (ex.Message, "#A4");
  551. Assert.IsNull (ex.ParamName, "#A5");
  552. }
  553. try {
  554. ab.DefineUnmanagedResource (version_res);
  555. Assert.Fail ("#B1");
  556. } catch (ArgumentException ex) {
  557. // Native resource has already been defined
  558. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  559. Assert.IsNull (ex.InnerException, "#B3");
  560. Assert.IsNotNull (ex.Message, "#B4");
  561. Assert.IsNull (ex.ParamName, "#B5");
  562. }
  563. try {
  564. ab.DefineVersionInfoResource ();
  565. Assert.Fail ("#C1");
  566. } catch (ArgumentException ex) {
  567. // Native resource has already been defined
  568. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  569. Assert.IsNull (ex.InnerException, "#C3");
  570. Assert.IsNotNull (ex.Message, "#C4");
  571. Assert.IsNull (ex.ParamName, "#C5");
  572. }
  573. try {
  574. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  575. Assert.Fail ("#D1");
  576. } catch (ArgumentException ex) {
  577. // Native resource has already been defined
  578. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  579. Assert.IsNull (ex.InnerException, "#D3");
  580. Assert.IsNotNull (ex.Message, "#D4");
  581. Assert.IsNull (ex.ParamName, "#D5");
  582. }
  583. }
  584. [Test] // DefineUnmanagedResource (byte [])
  585. public void TestDefineUnmanagedResource1_Resource_Null ()
  586. {
  587. try {
  588. ab.DefineUnmanagedResource ((byte []) null);
  589. Assert.Fail ("#1");
  590. } catch (ArgumentNullException ex) {
  591. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  592. Assert.IsNull (ex.InnerException, "#3");
  593. Assert.IsNotNull (ex.Message, "#4");
  594. Assert.IsNotNull (ex.ParamName, "#5");
  595. Assert.AreEqual ("resource", ex.ParamName, "#6");
  596. }
  597. }
  598. [Test] // DefineUnmanagedResource (String)
  599. public void TestDefineUnmanagedResource2_ResourceAlreadyDefined ()
  600. {
  601. string version_res = Path.Combine (tempDir, "version.res");
  602. using (FileStream fs = File.OpenWrite (version_res)) {
  603. fs.WriteByte (0x0a);
  604. }
  605. ab.DefineUnmanagedResource (version_res);
  606. try {
  607. ab.DefineUnmanagedResource (new byte [0]);
  608. Assert.Fail ("#A1");
  609. } catch (ArgumentException ex) {
  610. // Native resource has already been defined
  611. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  612. Assert.IsNull (ex.InnerException, "#A3");
  613. Assert.IsNotNull (ex.Message, "#A4");
  614. Assert.IsNull (ex.ParamName, "#A5");
  615. }
  616. try {
  617. ab.DefineUnmanagedResource (version_res);
  618. Assert.Fail ("#B1");
  619. } catch (ArgumentException ex) {
  620. // Native resource has already been defined
  621. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  622. Assert.IsNull (ex.InnerException, "#B3");
  623. Assert.IsNotNull (ex.Message, "#B4");
  624. Assert.IsNull (ex.ParamName, "#B5");
  625. }
  626. try {
  627. ab.DefineVersionInfoResource ();
  628. Assert.Fail ("#C1");
  629. } catch (ArgumentException ex) {
  630. // Native resource has already been defined
  631. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  632. Assert.IsNull (ex.InnerException, "#C3");
  633. Assert.IsNotNull (ex.Message, "#C4");
  634. Assert.IsNull (ex.ParamName, "#C5");
  635. }
  636. try {
  637. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  638. Assert.Fail ("#D1");
  639. } catch (ArgumentException ex) {
  640. // Native resource has already been defined
  641. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  642. Assert.IsNull (ex.InnerException, "#D3");
  643. Assert.IsNotNull (ex.Message, "#D4");
  644. Assert.IsNull (ex.ParamName, "#D5");
  645. }
  646. }
  647. [Test] // DefinedUnmanagedResource (String)
  648. [ExpectedException (typeof (FileNotFoundException))]
  649. public void TestDefineUnmanagedResource2_ResourceFile_DoesNotExist ()
  650. {
  651. ab.DefineUnmanagedResource ("not-exists.txt");
  652. }
  653. [Test] // DefinedUnmanagedResource (String)
  654. public void TestDefineUnmanagedResource2_ResourceFileName_Empty ()
  655. {
  656. try {
  657. ab.DefineUnmanagedResource (string.Empty);
  658. Assert.Fail ("#1");
  659. } catch (ArgumentException ex) {
  660. // The path is not of a legal form
  661. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
  662. Assert.IsNull (ex.InnerException, "#3");
  663. Assert.IsNotNull (ex.Message, "#4");
  664. Assert.IsNull (ex.ParamName, "#5");
  665. }
  666. }
  667. [Test] // DefinedUnmanagedResource (String)
  668. public void TestDefineUnmanagedResource2_ResourceFileName_Null ()
  669. {
  670. try {
  671. ab.DefineUnmanagedResource ((string) null);
  672. Assert.Fail ("#1");
  673. } catch (ArgumentNullException ex) {
  674. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  675. Assert.IsNull (ex.InnerException, "#3");
  676. Assert.IsNotNull (ex.Message, "#4");
  677. Assert.IsNotNull (ex.ParamName, "#5");
  678. Assert.AreEqual ("resourceFileName", ex.ParamName, "#6");
  679. }
  680. }
  681. [Test] // DefineVersionInfoResource ()
  682. public void TestDefineVersionInfoResource1_Culture_NotSupported ()
  683. {
  684. AssemblyName aname = new AssemblyName ();
  685. aname.CultureInfo = new CultureInfo ("nl-BE");
  686. aname.Name = "lib";
  687. aname.Version = new Version (3, 5, 7);
  688. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  689. aname, AssemblyBuilderAccess.RunAndSave,
  690. tempDir);
  691. // AssemblyCulture
  692. Type attrType = typeof (AssemblyCultureAttribute);
  693. ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
  694. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  695. ci, new object [1] { "doesnotexist" });
  696. ab.SetCustomAttribute (cab);
  697. ab.DefineVersionInfoResource ();
  698. try {
  699. ab.Save ("lib.dll");
  700. Assert.Fail ("#A1");
  701. } catch (CultureNotFoundException ex) {
  702. }
  703. ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
  704. AssemblyBuilderAccess.RunAndSave, tempDir);
  705. // AssemblyCulture
  706. attrType = typeof (AssemblyCultureAttribute);
  707. ci = attrType.GetConstructor (new Type [] { typeof (String) });
  708. cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
  709. ab.SetCustomAttribute (cab);
  710. ab.DefineVersionInfoResource ();
  711. try {
  712. ab.Save ("lib.dll");
  713. Assert.Fail ("#B1");
  714. } catch (CultureNotFoundException ex) {
  715. }
  716. }
  717. [Test] // DefineVersionInfoResource ()
  718. public void TestDefineVersionInfoResource1_ResourceAlreadyDefined ()
  719. {
  720. string version_res = Path.Combine (tempDir, "version.res");
  721. using (FileStream fs = File.OpenWrite (version_res)) {
  722. fs.WriteByte (0x0a);
  723. }
  724. ab.DefineVersionInfoResource ();
  725. try {
  726. ab.DefineUnmanagedResource (new byte [0]);
  727. Assert.Fail ("#A1");
  728. } catch (ArgumentException ex) {
  729. // Native resource has already been defined
  730. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  731. Assert.IsNull (ex.InnerException, "#A3");
  732. Assert.IsNotNull (ex.Message, "#A4");
  733. Assert.IsNull (ex.ParamName, "#A5");
  734. }
  735. try {
  736. ab.DefineUnmanagedResource (version_res);
  737. Assert.Fail ("#B1");
  738. } catch (ArgumentException ex) {
  739. // Native resource has already been defined
  740. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  741. Assert.IsNull (ex.InnerException, "#B3");
  742. Assert.IsNotNull (ex.Message, "#B4");
  743. Assert.IsNull (ex.ParamName, "#B5");
  744. }
  745. try {
  746. ab.DefineVersionInfoResource ();
  747. Assert.Fail ("#C1");
  748. } catch (ArgumentException ex) {
  749. // Native resource has already been defined
  750. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  751. Assert.IsNull (ex.InnerException, "#C3");
  752. Assert.IsNotNull (ex.Message, "#C4");
  753. Assert.IsNull (ex.ParamName, "#C5");
  754. }
  755. try {
  756. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  757. Assert.Fail ("#D1");
  758. } catch (ArgumentException ex) {
  759. // Native resource has already been defined
  760. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  761. Assert.IsNull (ex.InnerException, "#D3");
  762. Assert.IsNotNull (ex.Message, "#D4");
  763. Assert.IsNull (ex.ParamName, "#D5");
  764. }
  765. }
  766. [Test] // DefineVersionInfoResource (String, String, String, String, String)
  767. public void TestDefineVersionInfoResource2_Culture_NotSupported ()
  768. {
  769. AssemblyName aname = new AssemblyName ();
  770. aname.CultureInfo = new CultureInfo ("nl-BE");
  771. aname.Name = "lib";
  772. aname.Version = new Version (3, 5, 7);
  773. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  774. aname, AssemblyBuilderAccess.RunAndSave,
  775. tempDir);
  776. // AssemblyCulture
  777. Type attrType = typeof (AssemblyCultureAttribute);
  778. ConstructorInfo ci = attrType.GetConstructor (new Type [] { typeof (String) });
  779. CustomAttributeBuilder cab = new CustomAttributeBuilder (
  780. ci, new object [1] { "doesnotexist" });
  781. ab.SetCustomAttribute (cab);
  782. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  783. try {
  784. ab.Save ("lib.dll");
  785. Assert.Fail ("#A1");
  786. } catch (CultureNotFoundException ex) {
  787. }
  788. ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname,
  789. AssemblyBuilderAccess.RunAndSave, tempDir);
  790. // AssemblyCulture
  791. attrType = typeof (AssemblyCultureAttribute);
  792. ci = attrType.GetConstructor (new Type [] { typeof (String) });
  793. cab = new CustomAttributeBuilder (ci, new object [1] { "neutral" });
  794. ab.SetCustomAttribute (cab);
  795. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  796. try {
  797. ab.Save ("lib.dll");
  798. Assert.Fail ("#B1");
  799. } catch (CultureNotFoundException ex) {
  800. }
  801. }
  802. [Test] // DefineVersionInfoResource (String, String, String, String, String)
  803. public void TestDefineVersionInfoResource2_ResourceAlreadyDefined ()
  804. {
  805. string version_res = Path.Combine (tempDir, "version.res");
  806. using (FileStream fs = File.OpenWrite (version_res)) {
  807. fs.WriteByte (0x0a);
  808. }
  809. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  810. try {
  811. ab.DefineUnmanagedResource (new byte [0]);
  812. Assert.Fail ("#A1");
  813. } catch (ArgumentException ex) {
  814. // Native resource has already been defined
  815. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
  816. Assert.IsNull (ex.InnerException, "#A3");
  817. Assert.IsNotNull (ex.Message, "#A4");
  818. Assert.IsNull (ex.ParamName, "#A5");
  819. }
  820. try {
  821. ab.DefineUnmanagedResource (version_res);
  822. Assert.Fail ("#B1");
  823. } catch (ArgumentException ex) {
  824. // Native resource has already been defined
  825. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
  826. Assert.IsNull (ex.InnerException, "#B3");
  827. Assert.IsNotNull (ex.Message, "#B4");
  828. Assert.IsNull (ex.ParamName, "#B5");
  829. }
  830. try {
  831. ab.DefineVersionInfoResource ();
  832. Assert.Fail ("#C1");
  833. } catch (ArgumentException ex) {
  834. // Native resource has already been defined
  835. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
  836. Assert.IsNull (ex.InnerException, "#C3");
  837. Assert.IsNotNull (ex.Message, "#C4");
  838. Assert.IsNull (ex.ParamName, "#C5");
  839. }
  840. try {
  841. ab.DefineVersionInfoResource ("A", "1.0", "C", "D", "E");
  842. Assert.Fail ("#D1");
  843. } catch (ArgumentException ex) {
  844. // Native resource has already been defined
  845. Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
  846. Assert.IsNull (ex.InnerException, "#D3");
  847. Assert.IsNotNull (ex.Message, "#D4");
  848. Assert.IsNull (ex.ParamName, "#D5");
  849. }
  850. }
  851. [Test]
  852. public void TestSetCustomAttribute1_CustomBuilder_Null ()
  853. {
  854. try {
  855. ab.SetCustomAttribute (null);
  856. Assert.Fail ("#1");
  857. } catch (ArgumentNullException ex) {
  858. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  859. Assert.IsNull (ex.InnerException, "#3");
  860. Assert.IsNotNull (ex.Message, "#4");
  861. Assert.IsNotNull (ex.ParamName, "#5");
  862. Assert.AreEqual ("customBuilder", ex.ParamName, "#6");
  863. }
  864. }
  865. [Test]
  866. public void TestSetCustomAttribute2_ConstructorInfo_Null ()
  867. {
  868. try {
  869. ab.SetCustomAttribute (null, new byte [0]);
  870. Assert.Fail ("#1");
  871. } catch (ArgumentNullException ex) {
  872. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  873. Assert.IsNull (ex.InnerException, "#3");
  874. Assert.IsNotNull (ex.Message, "#4");
  875. Assert.IsNotNull (ex.ParamName, "#5");
  876. Assert.AreEqual ("con", ex.ParamName, "#6");
  877. }
  878. }
  879. [Test]
  880. public void TestSetCustomAttribute2_BinaryAttribute_Null ()
  881. {
  882. try {
  883. ab.SetCustomAttribute (typeof (AssemblyCompanyAttribute).GetConstructor (
  884. new Type [] { typeof (String) }), null);
  885. Assert.Fail ("#1");
  886. } catch (ArgumentNullException ex) {
  887. Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
  888. Assert.IsNull (ex.InnerException, "#3");
  889. Assert.IsNotNull (ex.Message, "#4");
  890. Assert.IsNotNull (ex.ParamName, "#5");
  891. Assert.AreEqual ("binaryAttribute", ex.ParamName, "#6");
  892. }
  893. }
  894. [Test] // SetCustomAttribute (CustomAttributeBuilder)
  895. public void TestSetCustomAttribute1 ()
  896. {
  897. Assembly a;
  898. AssemblyName an;
  899. AssemblyName check;
  900. Attribute attr;
  901. string filename;
  902. an = new AssemblyName ();
  903. an.Name = "TestSetCustomAttributeA";
  904. ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
  905. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
  906. GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4"}));
  907. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
  908. GetConstructor (new Type [] { typeof (string) }), new object [] { "bar"}));
  909. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
  910. GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
  911. new object [] { AssemblyHashAlgorithm.MD5 }));
  912. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
  913. GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint)0xff }));
  914. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
  915. GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  916. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
  917. GetConstructor (Type.EmptyTypes), new object [0]));
  918. ab.Save ("TestSetCustomAttributeA.dll");
  919. filename = Path.Combine (tempDir, "TestSetCustomAttributeA.dll");
  920. check = AssemblyName.GetAssemblyName (filename);
  921. Assert.AreEqual (CultureInfo.InvariantCulture, check.CultureInfo, "#A1");
  922. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#A2");
  923. Assert.AreEqual ("TestSetCustomAttributeA, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", check.FullName, "#A3");
  924. Assert.IsNull (check.GetPublicKey (), "#A4");
  925. Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#A5");
  926. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#A6");
  927. Assert.IsNull (check.KeyPair, "#A7");
  928. Assert.AreEqual ("TestSetCustomAttributeA", check.Name, "#A8");
  929. //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#A9");
  930. Assert.AreEqual (new Version (0, 0, 0, 0), check.Version, "#A10");
  931. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#A11");
  932. using (FileStream fs = File.OpenRead (filename)) {
  933. byte [] buffer = new byte [fs.Length];
  934. fs.Read (buffer, 0, buffer.Length);
  935. a = Assembly.Load (buffer);
  936. }
  937. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
  938. Assert.IsNotNull (attr, "#A12a");
  939. Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#A12b");
  940. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
  941. Assert.IsNotNull (attr, "#A13a");
  942. Assert.AreEqual ("bar", ((AssemblyCultureAttribute) attr).Culture, "#A13b");
  943. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
  944. Assert.IsNotNull (attr, "#A14a");
  945. Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#A14b");
  946. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
  947. Assert.IsNotNull (attr, "#A15a");
  948. Assert.AreEqual ((uint) 0xff, ((AssemblyFlagsAttribute) attr).Flags, "#A15b");
  949. attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
  950. Assert.IsNotNull (attr, "#A16");
  951. an = new AssemblyName ();
  952. an.CultureInfo = new CultureInfo ("nl-BE");
  953. an.Flags = AssemblyNameFlags.Retargetable;
  954. an.Name = "TestSetCustomAttributeB";
  955. an.ProcessorArchitecture = ProcessorArchitecture.IA64;
  956. an.Version = new Version (1, 3, 5, 7);
  957. an.VersionCompatibility = AssemblyVersionCompatibility.SameDomain;
  958. ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, tempDir);
  959. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyVersionAttribute).
  960. GetConstructor (new Type [] { typeof (string) }), new object [] { "1.2.3.4" }));
  961. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCultureAttribute).
  962. GetConstructor (new Type [] { typeof (string) }), new object [] { "en-US" }));
  963. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyAlgorithmIdAttribute).
  964. GetConstructor (new Type [] { typeof (AssemblyHashAlgorithm) }),
  965. new object [] { AssemblyHashAlgorithm.MD5 }));
  966. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyFlagsAttribute).
  967. GetConstructor (new Type [] { typeof (uint) }), new object [] { (uint) 0x0100 }));
  968. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).
  969. GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  970. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (FooAttribute).
  971. GetConstructor (Type.EmptyTypes), new object [0]));
  972. ab.Save ("TestSetCustomAttributeB.dll");
  973. filename = Path.Combine (tempDir, "TestSetCustomAttributeB.dll");
  974. check = AssemblyName.GetAssemblyName (filename);
  975. Assert.AreEqual ("nl-BE", check.CultureInfo.Name, "#B1");
  976. Assert.AreEqual (AssemblyNameFlags.Retargetable, check.Flags, "#B2");
  977. Assert.AreEqual ("TestSetCustomAttributeB, Version=1.3.5.7, Culture=nl-BE, PublicKeyToken=null, Retargetable=Yes", check.FullName, "#B3");
  978. Assert.IsNull (check.GetPublicKey (), "#B4");
  979. Assert.AreEqual (new byte [0], check.GetPublicKeyToken (), "#B5");
  980. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, check.HashAlgorithm, "#B6");
  981. Assert.IsNull (check.KeyPair, "#B7");
  982. Assert.AreEqual ("TestSetCustomAttributeB", check.Name, "#B8");
  983. //Assert.AreEqual (ProcessorArchitecture.MSIL, check.ProcessorArchitecture, "#B9");
  984. Assert.AreEqual (new Version (1, 3, 5, 7), check.Version, "#B10");
  985. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine, check.VersionCompatibility, "#B11");
  986. using (FileStream fs = File.OpenRead (filename)) {
  987. byte [] buffer = new byte [fs.Length];
  988. fs.Read (buffer, 0, buffer.Length);
  989. a = Assembly.Load (buffer);
  990. }
  991. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyVersionAttribute));
  992. Assert.IsNotNull (attr, "#B12a");
  993. Assert.AreEqual ("1.2.3.4", ((AssemblyVersionAttribute) attr).Version, "#B12b");
  994. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyCultureAttribute));
  995. Assert.IsNotNull (attr, "#B13a");
  996. Assert.AreEqual ("en-US", ((AssemblyCultureAttribute) attr).Culture, "#B13b");
  997. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyAlgorithmIdAttribute));
  998. Assert.IsNotNull (attr, "#B14a");
  999. Assert.AreEqual ((uint) AssemblyHashAlgorithm.MD5, ((AssemblyAlgorithmIdAttribute) attr).AlgorithmId, "#B14b");
  1000. attr = Attribute.GetCustomAttribute (a, typeof (AssemblyFlagsAttribute));
  1001. Assert.IsNotNull (attr, "#B15a");
  1002. Assert.AreEqual ((uint) 0x0100, ((AssemblyFlagsAttribute) attr).Flags, "#B15b");
  1003. attr = Attribute.GetCustomAttribute (a, typeof (FooAttribute));
  1004. Assert.IsNotNull (attr, "#B16");
  1005. }
  1006. // strongname generated using "sn -k unit.snk"
  1007. static byte[] strongName = {
  1008. 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
  1009. 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x7F, 0x7C, 0xEA, 0x4A,
  1010. 0x28, 0x33, 0xD8, 0x3C, 0x86, 0x90, 0x86, 0x91, 0x11, 0xBB, 0x30, 0x0D,
  1011. 0x3D, 0x69, 0x04, 0x4C, 0x48, 0xF5, 0x4F, 0xE7, 0x64, 0xA5, 0x82, 0x72,
  1012. 0x5A, 0x92, 0xC4, 0x3D, 0xC5, 0x90, 0x93, 0x41, 0xC9, 0x1D, 0x34, 0x16,
  1013. 0x72, 0x2B, 0x85, 0xC1, 0xF3, 0x99, 0x62, 0x07, 0x32, 0x98, 0xB7, 0xE4,
  1014. 0xFA, 0x75, 0x81, 0x8D, 0x08, 0xB9, 0xFD, 0xDB, 0x00, 0x25, 0x30, 0xC4,
  1015. 0x89, 0x13, 0xB6, 0x43, 0xE8, 0xCC, 0xBE, 0x03, 0x2E, 0x1A, 0x6A, 0x4D,
  1016. 0x36, 0xB1, 0xEB, 0x49, 0x26, 0x6C, 0xAB, 0xC4, 0x29, 0xD7, 0x8F, 0x25,
  1017. 0x11, 0xA4, 0x7C, 0x81, 0x61, 0x97, 0xCB, 0x44, 0x2D, 0x80, 0x49, 0x93,
  1018. 0x48, 0xA7, 0xC9, 0xAB, 0xDB, 0xCF, 0xA3, 0x34, 0xCB, 0x6B, 0x86, 0xE0,
  1019. 0x4D, 0x27, 0xFC, 0xA7, 0x4F, 0x36, 0xCA, 0x13, 0x42, 0xD3, 0x83, 0xC4,
  1020. 0x06, 0x6E, 0x12, 0xE0, 0xA1, 0x3D, 0x9F, 0xA9, 0xEC, 0xD1, 0xC6, 0x08,
  1021. 0x1B, 0x3D, 0xF5, 0xDB, 0x4C, 0xD4, 0xF0, 0x2C, 0xAA, 0xFC, 0xBA, 0x18,
  1022. 0x6F, 0x48, 0x7E, 0xB9, 0x47, 0x68, 0x2E, 0xF6, 0x1E, 0x67, 0x1C, 0x7E,
  1023. 0x0A, 0xCE, 0x10, 0x07, 0xC0, 0x0C, 0xAD, 0x5E, 0xC1, 0x53, 0x70, 0xD5,
  1024. 0xE7, 0x25, 0xCA, 0x37, 0x5E, 0x49, 0x59, 0xD0, 0x67, 0x2A, 0xBE, 0x92,
  1025. 0x36, 0x86, 0x8A, 0xBF, 0x3E, 0x17, 0x04, 0xFB, 0x1F, 0x46, 0xC8, 0x10,
  1026. 0x5C, 0x93, 0x02, 0x43, 0x14, 0x96, 0x6A, 0xD9, 0x87, 0x17, 0x62, 0x7D,
  1027. 0x3A, 0x45, 0xBE, 0x35, 0xDE, 0x75, 0x0B, 0x2A, 0xCE, 0x7D, 0xF3, 0x19,
  1028. 0x85, 0x4B, 0x0D, 0x6F, 0x8D, 0x15, 0xA3, 0x60, 0x61, 0x28, 0x55, 0x46,
  1029. 0xCE, 0x78, 0x31, 0x04, 0x18, 0x3C, 0x56, 0x4A, 0x3F, 0xA4, 0xC9, 0xB1,
  1030. 0x41, 0xED, 0x22, 0x80, 0xA1, 0xB3, 0xE2, 0xC7, 0x1B, 0x62, 0x85, 0xE4,
  1031. 0x81, 0x39, 0xCB, 0x1F, 0x95, 0xCC, 0x61, 0x61, 0xDF, 0xDE, 0xF3, 0x05,
  1032. 0x68, 0xB9, 0x7D, 0x4F, 0xFF, 0xF3, 0xC0, 0x0A, 0x25, 0x62, 0xD9, 0x8A,
  1033. 0x8A, 0x9E, 0x99, 0x0B, 0xFB, 0x85, 0x27, 0x8D, 0xF6, 0xD4, 0xE1, 0xB9,
  1034. 0xDE, 0xB4, 0x16, 0xBD, 0xDF, 0x6A, 0x25, 0x9C, 0xAC, 0xCD, 0x91, 0xF7,
  1035. 0xCB, 0xC1, 0x81, 0x22, 0x0D, 0xF4, 0x7E, 0xEC, 0x0C, 0x84, 0x13, 0x5A,
  1036. 0x74, 0x59, 0x3F, 0x3E, 0x61, 0x00, 0xD6, 0xB5, 0x4A, 0xA1, 0x04, 0xB5,
  1037. 0xA7, 0x1C, 0x29, 0xD0, 0xE1, 0x11, 0x19, 0xD7, 0x80, 0x5C, 0xEE, 0x08,
  1038. 0x15, 0xEB, 0xC9, 0xA8, 0x98, 0xF5, 0xA0, 0xF0, 0x92, 0x2A, 0xB0, 0xD3,
  1039. 0xC7, 0x8C, 0x8D, 0xBB, 0x88, 0x96, 0x4F, 0x18, 0xF0, 0x8A, 0xF9, 0x31,
  1040. 0x9E, 0x44, 0x94, 0x75, 0x6F, 0x78, 0x04, 0x10, 0xEC, 0xF3, 0xB0, 0xCE,
  1041. 0xA0, 0xBE, 0x7B, 0x25, 0xE1, 0xF7, 0x8A, 0xA8, 0xD4, 0x63, 0xC2, 0x65,
  1042. 0x47, 0xCC, 0x5C, 0xED, 0x7D, 0x8B, 0x07, 0x4D, 0x76, 0x29, 0x53, 0xAC,
  1043. 0x27, 0x8F, 0x5D, 0x78, 0x56, 0xFA, 0x99, 0x45, 0xA2, 0xCC, 0x65, 0xC4,
  1044. 0x54, 0x13, 0x9F, 0x38, 0x41, 0x7A, 0x61, 0x0E, 0x0D, 0x34, 0xBC, 0x11,
  1045. 0xAF, 0xE2, 0xF1, 0x8B, 0xFA, 0x2B, 0x54, 0x6C, 0xA3, 0x6C, 0x09, 0x1F,
  1046. 0x0B, 0x43, 0x9B, 0x07, 0x95, 0x83, 0x3F, 0x97, 0x99, 0x89, 0xF5, 0x51,
  1047. 0x41, 0xF6, 0x8E, 0x5D, 0xEF, 0x6D, 0x24, 0x71, 0x41, 0x7A, 0xAF, 0xBE,
  1048. 0x81, 0x71, 0xAB, 0x76, 0x2F, 0x1A, 0x5A, 0xBA, 0xF3, 0xA6, 0x65, 0x7A,
  1049. 0x80, 0x50, 0xCE, 0x23, 0xC3, 0xC7, 0x53, 0xB0, 0x7C, 0x97, 0x77, 0x27,
  1050. 0x70, 0x98, 0xAE, 0xB5, 0x24, 0x66, 0xE1, 0x60, 0x39, 0x41, 0xDA, 0x54,
  1051. 0x01, 0x64, 0xFB, 0x10, 0x33, 0xCE, 0x8B, 0xBE, 0x27, 0xD4, 0x21, 0x57,
  1052. 0xCC, 0x0F, 0x1A, 0xC1, 0x3D, 0xF3, 0xCC, 0x39, 0xF0, 0x2F, 0xAE, 0xF1,
  1053. 0xC0, 0xCD, 0x3B, 0x23, 0x87, 0x49, 0x7E, 0x40, 0x32, 0x6A, 0xD3, 0x96,
  1054. 0x4A, 0xE5, 0x5E, 0x6E, 0x26, 0xFD, 0x8A, 0xCF, 0x7E, 0xFC, 0x37, 0xDE,
  1055. 0x39, 0x0C, 0x53, 0x81, 0x75, 0x08, 0xAF, 0x6B, 0x39, 0x6C, 0xFB, 0xC9,
  1056. 0x79, 0xC0, 0x9B, 0x5F, 0x34, 0x86, 0xB2, 0xDE, 0xC4, 0x19, 0x84, 0x5F,
  1057. 0x0E, 0xED, 0x9B, 0xB8, 0xD3, 0x17, 0xDA, 0x78 };
  1058. static byte [] token = { 0x0e, 0xea, 0x7c, 0xe6, 0x5f, 0x35, 0xf2, 0xd8 };
  1059. [Test]
  1060. public void StrongName_MissingKeyFile_NoDelay ()
  1061. {
  1062. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { "missing.snk" }));
  1063. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { false }));
  1064. ab.Save ("StrongName_MissingKeyFile_NoDelay.dll");
  1065. string filename = Path.Combine (tempDir, "StrongName_MissingKeyFile_NoDelay.dll");
  1066. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1067. // no exception is thrown (file not found)
  1068. // because it's not AssemblyBuilder.Save job to do the signing :-/
  1069. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
  1070. Assert.IsNull (check.GetPublicKey (), "#2");
  1071. Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
  1072. Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
  1073. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
  1074. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
  1075. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
  1076. }
  1077. [Test]
  1078. public void StrongName_KeyFile_Delay ()
  1079. {
  1080. string strongfile = Path.Combine (tempDir, "strongname.snk");
  1081. using (FileStream fs = File.OpenWrite (strongfile)) {
  1082. fs.Write (strongName, 0, strongName.Length);
  1083. fs.Close ();
  1084. }
  1085. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyKeyFileAttribute).GetConstructor (new Type [] { typeof (string) }), new object [] { strongfile }));
  1086. ab.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDelaySignAttribute).GetConstructor (new Type [] { typeof (bool) }), new object [] { true }));
  1087. ab.Save ("StrongName_KeyFile_Delay.dll");
  1088. string filename = Path.Combine (tempDir, "StrongName_KeyFile_Delay.dll");
  1089. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1090. // no public key is inserted into the assembly
  1091. // because it's not AssemblyBuilder.Save job to do the signing :-/
  1092. Assert.AreEqual (AssemblyNameFlags.None, check.Flags, "#1");
  1093. Assert.IsNull (check.GetPublicKey (), "#2");
  1094. Assert.IsNotNull (check.GetPublicKeyToken (), "#3a");
  1095. Assert.AreEqual (0, check.GetPublicKeyToken ().Length, "#3b");
  1096. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#4");
  1097. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#5");
  1098. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=null") != -1, "#6");
  1099. }
  1100. [Test]
  1101. public void StrongName_WithoutAttributes ()
  1102. {
  1103. // this demonstrate that AssemblyKeyFileAttribute (or AssemblyKeyNameAttribute)
  1104. // aren't required to sign an assembly.
  1105. AssemblyName an = genAssemblyName ();
  1106. an.KeyPair = new StrongNameKeyPair (strongName);
  1107. AssemblyBuilder ab = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
  1108. ab.Save ("StrongName_WithoutAttributes.dll");
  1109. string filename = Path.Combine (tempDir, "StrongName_WithoutAttributes.dll");
  1110. AssemblyName check = AssemblyName.GetAssemblyName (filename);
  1111. Assert.IsNotNull (check.GetPublicKey (), "#1a");
  1112. Assert.IsTrue (check.GetPublicKey ().Length > 0, "#1b");
  1113. Assert.AreEqual ("0E-EA-7C-E6-5F-35-F2-D8", BitConverter.ToString (check.GetPublicKeyToken ()), "#2");
  1114. Assert.IsTrue (check.FullName.IndexOf ("Version=0.0.0.0") != -1, "#3");
  1115. Assert.IsTrue (check.FullName.IndexOf ("Culture=neutral") != -1, "#4");
  1116. Assert.IsTrue (check.FullName.IndexOf ("PublicKeyToken=0eea7ce65f35f2d8") != -1, "#5");
  1117. Assert.AreEqual (AssemblyNameFlags.PublicKey, check.Flags, "#6");
  1118. }
  1119. [Test]
  1120. public void SaveUnfinishedTypes ()
  1121. {
  1122. mb.DefineType ("TestType", TypeAttributes.Class |
  1123. TypeAttributes.Public | TypeAttributes.Sealed |
  1124. TypeAttributes.AnsiClass | TypeAttributes.AutoClass,
  1125. typeof(object));
  1126. try {
  1127. ab.Save ("def_module");
  1128. Assert.Fail ("#1");
  1129. } catch (NotSupportedException ex) {
  1130. // Type 'TestType' was not completed
  1131. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  1132. Assert.IsNull (ex.InnerException, "#3");
  1133. Assert.IsNotNull (ex.Message, "#4");
  1134. Assert.IsTrue (ex.Message.IndexOf ("TestType") != -1, "#5");
  1135. }
  1136. }
  1137. [Test]
  1138. public void GetModules ()
  1139. {
  1140. Module[] arr;
  1141. arr = ab.GetModules ();
  1142. Assert.IsNotNull (arr, "#A1");
  1143. // FIXME: This doesn't work on mono
  1144. //Assert.IsTrue (arr.Length >= 2, "#A2");
  1145. foreach (Module m in arr)
  1146. Assert.AreEqual (typeof (ModuleBuilder), m.GetType (), "#A3");
  1147. // Test with no modules
  1148. AssemblyBuilder ab2 = genAssembly ();
  1149. arr = ab2.GetModules ();
  1150. Assert.IsNotNull (arr, "#B1");
  1151. Assert.AreEqual (0, arr.Length, "#B2");
  1152. }
  1153. [Test]
  1154. [Category ("NotWorking")] // bug #351932
  1155. public void GetReferencedAssemblies ()
  1156. {
  1157. AssemblyBuilder ab1;
  1158. AssemblyBuilder ab2;
  1159. AssemblyBuilder ab3;
  1160. AssemblyName [] refs;
  1161. TypeBuilder tb1;
  1162. TypeBuilder tb2;
  1163. TypeBuilder tb3;
  1164. TypeBuilder tb4;
  1165. ModuleBuilder mb1;
  1166. ModuleBuilder mb2;
  1167. ModuleBuilder mb3;
  1168. AssemblyName an1 = genAssemblyName ();
  1169. an1.Version = new Version (3, 0);
  1170. AssemblyName an2 = genAssemblyName ();
  1171. an2.Version = new Version ("1.2.3.4");
  1172. an2.KeyPair = new StrongNameKeyPair (strongName);
  1173. AssemblyName an3 = genAssemblyName ();
  1174. ab1 = domain.DefineDynamicAssembly (an1,
  1175. AssemblyBuilderAccess.RunAndSave,
  1176. tempDir);
  1177. ab2 = domain.DefineDynamicAssembly (an2,
  1178. AssemblyBuilderAccess.RunAndSave,
  1179. tempDir);
  1180. ab3 = domain.DefineDynamicAssembly (an3,
  1181. AssemblyBuilderAccess.RunAndSave,
  1182. tempDir);
  1183. refs = ab1.GetReferencedAssemblies ();
  1184. Assert.AreEqual (0, refs.Length, "#A1");
  1185. refs = ab2.GetReferencedAssemblies ();
  1186. Assert.AreEqual (0, refs.Length, "#A2");
  1187. refs = ab3.GetReferencedAssemblies ();
  1188. Assert.AreEqual (0, refs.Length, "#A3");
  1189. mb1 = ab1.DefineDynamicModule (an1.Name + ".dll");
  1190. tb1 = mb1.DefineType ("TestType1", TypeAttributes.Class |
  1191. TypeAttributes.Public, typeof (Attribute));
  1192. tb1.CreateType ();
  1193. mb2 = ab2.DefineDynamicModule (an2.Name + ".dll");
  1194. tb2 = mb2.DefineType ("TestType2", TypeAttributes.Class |
  1195. TypeAttributes.Public, tb1);
  1196. tb2.CreateType ();
  1197. mb3 = ab3.DefineDynamicModule (an3.Name + ".dll");
  1198. tb3 = mb3.DefineType ("TestType3", TypeAttributes.Class |
  1199. TypeAttributes.Public, tb1);
  1200. tb3.CreateType ();
  1201. tb4 = mb3.DefineType ("TestType4", TypeAttributes.Class |
  1202. TypeAttributes.Public, tb2);
  1203. tb4.CreateType ();
  1204. refs = ab1.GetReferencedAssemblies ();
  1205. Assert.AreEqual (0, refs.Length, "#B1");
  1206. refs = ab2.GetReferencedAssemblies ();
  1207. Assert.AreEqual (0, refs.Length, "#B2");
  1208. refs = ab3.GetReferencedAssemblies ();
  1209. Assert.AreEqual (0, refs.Length, "#B3");
  1210. ab1.Save (an1.Name + ".dll");
  1211. ab2.Save (an2.Name + ".dll");
  1212. ab3.Save (an3.Name + ".dll");
  1213. refs = ab1.GetReferencedAssemblies ();
  1214. Assert.AreEqual (0, refs.Length, "#C1");
  1215. refs = ab2.GetReferencedAssemblies ();
  1216. Assert.AreEqual (0, refs.Length, "#C2");
  1217. refs = ab3.GetReferencedAssemblies ();
  1218. Assert.AreEqual (0, refs.Length, "#C3");
  1219. string assemblyFile = Path.Combine (tempDir, an1.Name + ".dll");
  1220. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1221. byte [] buffer = new byte [fs.Length];
  1222. fs.Read (buffer, 0, buffer.Length);
  1223. Assembly a = Assembly.Load (buffer);
  1224. refs = a.GetReferencedAssemblies ();
  1225. Assert.AreEqual (1, refs.Length, "#D1");
  1226. Assert.IsNull (refs [0].CodeBase, "#D2:CodeBase");
  1227. Assert.IsNotNull (refs [0].CultureInfo, "#D2:CultureInfo");
  1228. Assert.IsNull (refs [0].EscapedCodeBase, "#D2:EscapedCodeBase");
  1229. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#D2:Flags");
  1230. Assert.AreEqual (typeof (object).FullName, refs [0].FullName, "#D2:FullName");
  1231. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#D2:HashAlgorithm");
  1232. Assert.IsNull (refs [0].KeyPair, "#D2:KeyPair");
  1233. Assert.AreEqual ("mscorlib", refs [0].Name, "#D2:Name");
  1234. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#D2:PA");
  1235. string FxVersion;
  1236. #if MOBILE
  1237. FxVersion = "2.0.5.0;";
  1238. #else
  1239. FxVersion = "4.0.0.0;";
  1240. #endif
  1241. Assert.AreEqual (new Version (FxVersion), refs [0].Version, "#D2:Version");
  1242. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1243. refs [0].VersionCompatibility, "#D2:VersionCompatibility");
  1244. Assert.IsNull (refs [0].GetPublicKey (), "#D2:GetPublicKey");
  1245. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#D2:GetPublicKeyToken(a)");
  1246. Assert.AreEqual (8, refs [0].GetPublicKeyToken ().Length, "#D2:GetPublicKeyToken(b)");
  1247. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#D2:ToString");
  1248. }
  1249. assemblyFile = Path.Combine (tempDir, an2.Name + ".dll");
  1250. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1251. byte [] buffer = new byte [fs.Length];
  1252. fs.Read (buffer, 0, buffer.Length);
  1253. Assembly a = Assembly.Load (buffer);
  1254. refs = a.GetReferencedAssemblies ();
  1255. Assert.AreEqual (1, refs.Length, "#E1");
  1256. Assert.IsNull (refs [0].CodeBase, "#E2:CodeBase");
  1257. Assert.IsNotNull (refs [0].CultureInfo, "#E2:CultureInfo(a)");
  1258. Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#E2:CultureInfo(b)");
  1259. Assert.IsNull (refs [0].EscapedCodeBase, "#E2:EscapedCodeBase");
  1260. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#E2:Flags");
  1261. Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#E2:FullName");
  1262. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#E2:HashAlgorithm");
  1263. Assert.IsNull (refs [0].KeyPair, "#E2:KeyPair");
  1264. Assert.AreEqual (an1.Name, refs [0].Name, "#E2:Name");
  1265. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#E2:PA");
  1266. Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#E2:Version");
  1267. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1268. refs [0].VersionCompatibility, "#E2:VersionCompatibility");
  1269. Assert.IsNull (refs [0].GetPublicKey (), "#E2:GetPublicKey");
  1270. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#E2:GetPublicKeyToken(a)");
  1271. Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#E2:GetPublicKeyToken(b)");
  1272. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#E2:ToString");
  1273. }
  1274. assemblyFile = Path.Combine (tempDir, an3.Name + ".dll");
  1275. using (FileStream fs = File.OpenRead (assemblyFile)) {
  1276. byte [] buffer = new byte [fs.Length];
  1277. fs.Read (buffer, 0, buffer.Length);
  1278. Assembly a = Assembly.Load (buffer);
  1279. refs = a.GetReferencedAssemblies ();
  1280. Assert.AreEqual (2, refs.Length, "#F1");
  1281. Assert.IsNull (refs [0].CodeBase, "#F2:CodeBase");
  1282. Assert.IsNotNull (refs [0].CultureInfo, "#F2:CultureInfo(a)");
  1283. Assert.AreEqual (CultureInfo.InvariantCulture, refs [0].CultureInfo, "#F2:CultureInfo(b)");
  1284. Assert.IsNull (refs [0].EscapedCodeBase, "#F2:EscapedCodeBase");
  1285. Assert.AreEqual (AssemblyNameFlags.None, refs [0].Flags, "#F2:Flags");
  1286. Assert.AreEqual (an1.Name + ", Version=3.0.0.0, Culture=neutral, PublicKeyToken=null", refs [0].FullName, "#F2:FullName");
  1287. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [0].HashAlgorithm, "#F2:HashAlgorithm");
  1288. Assert.IsNull (refs [0].KeyPair, "#F2:KeyPair");
  1289. Assert.AreEqual (an1.Name, refs [0].Name, "#F2:Name");
  1290. Assert.AreEqual (ProcessorArchitecture.None, refs [0].ProcessorArchitecture, "#F2:PA");
  1291. Assert.AreEqual (new Version (3, 0, 0, 0), refs [0].Version, "#F2:Version");
  1292. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1293. refs [0].VersionCompatibility, "#F2:VersionCompatibility");
  1294. Assert.IsNull (refs [0].GetPublicKey (), "#F2:GetPublicKey");
  1295. Assert.IsNotNull (refs [0].GetPublicKeyToken (), "#F2:GetPublicKeyToken(a)");
  1296. Assert.AreEqual (0, refs [0].GetPublicKeyToken ().Length, "#F2:GetPublicKeyToken(b)");
  1297. Assert.AreEqual (refs [0].FullName, refs [0].ToString (), "#F2:ToString");
  1298. Assert.IsNull (refs [1].CodeBase, "#F3:CodeBase");
  1299. Assert.IsNotNull (refs [1].CultureInfo, "#F3:CultureInfo(a)");
  1300. Assert.AreEqual (CultureInfo.InvariantCulture, refs [1].CultureInfo, "#F3:CultureInfo(b)");
  1301. Assert.IsNull (refs [1].EscapedCodeBase, "#F3:EscapedCodeBase");
  1302. Assert.AreEqual (AssemblyNameFlags.None, refs [1].Flags, "#F3:Flags");
  1303. Assert.AreEqual (an2.Name + ", Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8", refs [1].FullName, "#F3:FullName");
  1304. Assert.AreEqual (AssemblyHashAlgorithm.SHA1, refs [1].HashAlgorithm, "#F3:HashAlgorithm");
  1305. Assert.IsNull (refs [1].KeyPair, "#F3:KeyPair");
  1306. Assert.AreEqual (an2.Name, refs [1].Name, "#F3:Name");
  1307. Assert.AreEqual (ProcessorArchitecture.None, refs [1].ProcessorArchitecture, "#F3:PA");
  1308. Assert.AreEqual (new Version (1, 2, 3, 4), refs [1].Version, "#F3:Version");
  1309. Assert.AreEqual (AssemblyVersionCompatibility.SameMachine,
  1310. refs [1].VersionCompatibility, "#F3:VersionCompatibility");
  1311. Assert.IsNull (refs [1].GetPublicKey (), "#F3:GetPublicKey");
  1312. Assert.AreEqual (token, refs [1].GetPublicKeyToken (), "#F3:GetPublicKeyToken");
  1313. Assert.AreEqual (refs [1].FullName, refs [1].ToString (), "#F3:ToString");
  1314. }
  1315. }
  1316. [Test] // bug #78724
  1317. public void GetTypes ()
  1318. {
  1319. TypeBuilder tb = mb.DefineType ("sometype");
  1320. tb.CreateType ();
  1321. Type[] types = ab.GetTypes ();
  1322. Assert.AreEqual (1, types.Length, "#1");
  1323. Assert.AreEqual ("sometype", types[0].Name, "#2");
  1324. }
  1325. [Test]
  1326. public void AssemblyName_Culture ()
  1327. {
  1328. AssemblyName assemblyName = new AssemblyName ();
  1329. assemblyName.Name = "AssemblyNameTest";
  1330. assemblyName.Version = new Version ("1.0.0.0");
  1331. assemblyName.CultureInfo = new CultureInfo ("en-US");
  1332. const string fullName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US, PublicKeyToken=null";
  1333. const string abName = "AssemblyNameTest, Version=1.0.0.0, Culture=en-US";
  1334. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1335. }
  1336. [Test]
  1337. public void AssemblyName_PublicKey ()
  1338. {
  1339. AssemblyName assemblyName = new AssemblyName ();
  1340. assemblyName.Name = "AssemblyNameTest_PublicKey";
  1341. assemblyName.Version = new Version ("1.2.3.4");
  1342. assemblyName.KeyPair = new StrongNameKeyPair (strongName);
  1343. Assert.AreEqual ("AssemblyNameTest_PublicKey, Version=1.2.3.4", assemblyName.FullName, "#A1");
  1344. const string fullName = "AssemblyNameTest_PublicKey, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
  1345. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1346. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1347. AssemblyName abName = ab.GetName ();
  1348. Assert.AreEqual (CultureInfo.InvariantCulture, abName.CultureInfo, "#B1");
  1349. Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B2");
  1350. Assert.IsNotNull (abName.GetPublicKey () != null, "#B3a");
  1351. Assert.IsTrue (abName.GetPublicKey ().Length > 0, "#B3b");
  1352. Assert.IsNotNull (abName.GetPublicKeyToken (), "#B4a");
  1353. Assert.IsTrue (abName.GetPublicKeyToken ().Length > 0, "#B4b");
  1354. Assert.AreEqual (fullName, abName.FullName, "#B5");
  1355. ab.Save ("AssemblyNameTest_PublicKey.dll");
  1356. AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
  1357. tempDir, "AssemblyNameTest_PublicKey.dll"));
  1358. Assert.AreEqual (CultureInfo.InvariantCulture, bakedName.CultureInfo, "#C1");
  1359. Assert.AreEqual (AssemblyNameFlags.PublicKey, bakedName.Flags, "#C2");
  1360. Assert.IsNotNull (bakedName.GetPublicKeyToken (), "#C3");
  1361. Assert.IsNotNull (bakedName.GetPublicKey (), "#C4");
  1362. Assert.AreEqual (fullName, bakedName.FullName, "#C5");
  1363. }
  1364. [Test]
  1365. public void AssemblyName_MoreCultureInfo ()
  1366. {
  1367. AssemblyName assemblyName = new AssemblyName ();
  1368. assemblyName.Name = "AssemblyNameTest_MoreCultureInfo";
  1369. assemblyName.Version = new Version ("1.2.3.4");
  1370. assemblyName.KeyPair = new StrongNameKeyPair (strongName);
  1371. Assert.IsNull (assemblyName.CultureInfo, "#A1");
  1372. Assert.AreEqual ("AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4", assemblyName.FullName, "#A2");
  1373. const string fullName = "AssemblyNameTest_MoreCultureInfo, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0eea7ce65f35f2d8";
  1374. AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (
  1375. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1376. AssemblyName abName = ab.GetName ();
  1377. Assert.IsNotNull (abName.CultureInfo != null, "#B1");
  1378. Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#B2a");
  1379. Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#B2a");
  1380. Assert.AreEqual (AssemblyNameFlags.PublicKey, abName.Flags, "#B3");
  1381. Assert.AreEqual (fullName, abName.FullName, "#B4");
  1382. ab.Save ("AssemblyNameTest_MoreCultureInfo.dll");
  1383. AssemblyName bakedName = AssemblyName.GetAssemblyName (Path.Combine(
  1384. tempDir, "AssemblyNameTest_MoreCultureInfo.dll"));
  1385. Assert.IsNotNull (bakedName.CultureInfo, "#C1");
  1386. Assert.IsTrue (abName.CultureInfo != CultureInfo.InvariantCulture, "#C2a");
  1387. Assert.AreEqual (CultureInfo.InvariantCulture.LCID, abName.CultureInfo.LCID, "#C2b");
  1388. Assert.AreEqual (fullName, bakedName.FullName, "#C3");
  1389. }
  1390. [Test]
  1391. public void AssemblyName_NoVersion ()
  1392. {
  1393. AssemblyName assemblyName = new AssemblyName ();
  1394. assemblyName.Name = "AssemblyNameTest";
  1395. const string fullName = "AssemblyNameTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null";
  1396. const string abName = "AssemblyNameTest, Version=0.0.0.0";
  1397. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1398. }
  1399. [Test]
  1400. public void AssemblyName_Version ()
  1401. {
  1402. AssemblyName assemblyName = new AssemblyName ();
  1403. assemblyName.Name = "AssemblyNameTest";
  1404. assemblyName.Version = new Version (1, 2, 3, 4);
  1405. string fullName = "AssemblyNameTest, Version=1.2.3.4, Culture=neutral, PublicKeyToken=null";
  1406. string abName = "AssemblyNameTest, Version=1.2.3.4";
  1407. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1408. assemblyName = new AssemblyName ();
  1409. assemblyName.Name = "AssemblyNameTest";
  1410. assemblyName.Version = new Version (1, 2);
  1411. fullName = "AssemblyNameTest, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null";
  1412. abName = "AssemblyNameTest, Version=1.2.0.0";
  1413. AssertAssemblyName (tempDir, assemblyName, abName, fullName);
  1414. }
  1415. [Test]
  1416. public void GetType_IgnoreCase ()
  1417. {
  1418. TypeBuilder tb = mb.DefineType ("Foo.Test2", TypeAttributes.Public, typeof (object));
  1419. tb.CreateType ();
  1420. Type t;
  1421. t = ab.GetType ("foo.Test2", true, true);
  1422. Assert.AreEqual ("Test2", t.Name, "#1");
  1423. t = ab.GetType ("foo.test2", true, true);
  1424. Assert.AreEqual ("Test2", t.Name, "#2");
  1425. t = ab.GetType ("Foo.test2", true, true);
  1426. Assert.AreEqual ("Test2", t.Name, "#3");
  1427. }
  1428. [Test]
  1429. public void TestGetType ()
  1430. {
  1431. TypeBuilder tb = mb.DefineType ("Test", TypeAttributes.Public);
  1432. Assert.IsNull (ab.GetType ("Test", false, true), "#1");
  1433. try {
  1434. ab.GetType ("Test", true, true);
  1435. Assert.Fail ("#2");
  1436. } catch (TypeLoadException) { }
  1437. var res = tb.CreateType ();
  1438. Assert.AreSame (res, ab.GetType ("Test", false, true), "#3");
  1439. }
  1440. [Test]
  1441. public void GetModule ()
  1442. {
  1443. var ab = genAssembly ();
  1444. Assert.IsNull (ab.GetModule ("Foo"), "#1");
  1445. var modA = ab.DefineDynamicModule ("Foo");
  1446. var modB = ab.DefineDynamicModule ("Bar");
  1447. Assert.AreSame (modA, ab.GetModule ("Foo"), "#2");
  1448. Assert.AreSame (modB, ab.GetModule ("Bar"), "#3");
  1449. Assert.IsNull (ab.GetModule ("FooBar"), "#4");
  1450. }
  1451. [Test]
  1452. public void GetModules2 ()
  1453. {
  1454. //XXX this is not the v4 behavior since it returns
  1455. //the manifest module in the place of the first one
  1456. var ab = genAssembly ();
  1457. var modA = ab.DefineDynamicModule ("Foo");
  1458. var modB = ab.DefineDynamicModule ("Bar");
  1459. Assert.AreEqual (2, ab.GetModules ().Length, "#1");
  1460. Assert.AreSame (modA, ab.GetModules () [0], "#2");
  1461. Assert.AreSame (modB, ab.GetModules () [1], "#3");
  1462. }
  1463. [Test]
  1464. [Category ("NotDotNet")] // MS returns the real deal
  1465. public void GetReferencedAssemblies_Trivial ()
  1466. {
  1467. Assert.IsNotNull (ab.GetReferencedAssemblies (), "#1");
  1468. }
  1469. [Test]
  1470. public void GetLoadedModules ()
  1471. {
  1472. var res = ab.GetLoadedModules (true);
  1473. Assert.IsNotNull (res, "#1");
  1474. Assert.AreEqual (1, res.Length, "#2");
  1475. Assert.AreEqual (mb, res [0], "#3");
  1476. }
  1477. [ExpectedException (typeof (TypeLoadException))]
  1478. public void GetCustomAttributes_NotCreated ()
  1479. {
  1480. AssemblyBuilder ab = genAssembly ();
  1481. ModuleBuilder mb = ab.DefineDynamicModule("tester", "tester.dll", false);
  1482. TypeBuilder tb = mb.DefineType ("T");
  1483. tb.SetParent (typeof (Attribute));
  1484. ConstructorBuilder ctor = tb.DefineDefaultConstructor (MethodAttributes.Public);
  1485. object [] o = new object [0];
  1486. CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, o);
  1487. ab.SetCustomAttribute (cab);
  1488. ab.GetCustomAttributes (true);
  1489. }
  1490. [Test]
  1491. public void GetTypesWithUnfinishedTypeBuilder ()
  1492. {
  1493. AssemblyBuilder ab = genAssembly ();
  1494. ModuleBuilder mb = ab.DefineDynamicModule("tester", "tester.dll", false);
  1495. mb.DefineType ("K").CreateType ();
  1496. var tb = mb.DefineType ("T");
  1497. try {
  1498. ab.GetTypes ();
  1499. Assert.Fail ("#1");
  1500. } catch (ReflectionTypeLoadException ex) {
  1501. Assert.AreEqual (1, ex.Types.Length, "#2");
  1502. Assert.AreEqual (1, ex.LoaderExceptions.Length, "#3");
  1503. Assert.IsNull (ex.Types [0], "#4");
  1504. Assert.IsTrue (ex.LoaderExceptions [0] is TypeLoadException, "#5");
  1505. }
  1506. tb.CreateType ();
  1507. var types = ab.GetTypes ();
  1508. Assert.AreEqual (2, types.Length, "#5");
  1509. foreach (var t in types)
  1510. Assert.IsFalse (t is TypeBuilder, "#6_" + t.Name);
  1511. }
  1512. [Test]
  1513. public void DynamicAssemblyGenerationInCurrentDomainShouldNotChangeTheOrderOfCurrentDomainGetAssemblies ()
  1514. {
  1515. var initialPosition = GetAssemblyPositionForType (GetType ());
  1516. DefineDynamicAssembly (AppDomain.CurrentDomain);
  1517. var currentPosition = GetAssemblyPositionForType (GetType ());
  1518. Assert.AreEqual (initialPosition, currentPosition);
  1519. }
  1520. static void DefineDynamicAssembly (AppDomain domain)
  1521. {
  1522. AssemblyName assemblyName = new AssemblyName ();
  1523. assemblyName.Name = "MyDynamicAssembly";
  1524. AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
  1525. ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyDynamicModule");
  1526. TypeBuilder typeBuilder = moduleBuilder.DefineType ("MyDynamicType", TypeAttributes.Public);
  1527. ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, null);
  1528. ILGenerator ilGenerator = constructorBuilder.GetILGenerator ();
  1529. ilGenerator.EmitWriteLine ("MyDynamicType instantiated!");
  1530. ilGenerator.Emit (OpCodes.Ret);
  1531. typeBuilder.CreateType ();
  1532. }
  1533. static int GetAssemblyPositionForType (Type type)
  1534. {
  1535. var assemblies = AppDomain.CurrentDomain.GetAssemblies ();
  1536. for (int i = 0; i < assemblies.Length; i++)
  1537. if (type.Assembly == assemblies [i])
  1538. return i;
  1539. return -1;
  1540. }
  1541. private static void AssertAssemblyName (string tempDir, AssemblyName assemblyName, string abName, string fullName)
  1542. {
  1543. AppDomain currentDomain = AppDomain.CurrentDomain;
  1544. AppDomain newDomain = null;
  1545. try {
  1546. AssemblyBuilder ab = currentDomain.DefineDynamicAssembly (
  1547. assemblyName, AssemblyBuilderAccess.Save, tempDir);
  1548. ab.Save (assemblyName.Name + ".dll");
  1549. // on .NET 2.0, the full name of the AssemblyBuilder matches the
  1550. // fully qualified assembly name
  1551. Assert.AreEqual (fullName, ab.FullName, "#1");
  1552. AssemblyName an = ab.GetName ();
  1553. Assert.AreEqual (AssemblyNameFlags.PublicKey, an.Flags, "#2");
  1554. Assert.IsNotNull (an.GetPublicKey (), "#3a");
  1555. Assert.AreEqual (0, an.GetPublicKey ().Length, "#3b");
  1556. Assert.IsNotNull (an.GetPublicKeyToken (), "#4a");
  1557. Assert.AreEqual (0, an.GetPublicKeyToken ().Length, "#4b");
  1558. // load assembly in separate domain, so we can clean-up after the
  1559. // test
  1560. newDomain = AppDomain.CreateDomain ("test2", currentDomain.Evidence,
  1561. currentDomain.SetupInformation);
  1562. Helper helper = new Helper (Path.Combine (tempDir, assemblyName.Name + ".dll"),
  1563. fullName);
  1564. newDomain.DoCallBack (new CrossAppDomainDelegate (helper.Test));
  1565. } finally {
  1566. #if !MONODROID
  1567. // RUNTIME: crash
  1568. // AppDomain unloading crashes the runtime on Android
  1569. if (newDomain != null) {
  1570. AppDomain.Unload (newDomain);
  1571. }
  1572. #endif
  1573. }
  1574. }
  1575. [Serializable ()]
  1576. private class Helper
  1577. {
  1578. private readonly string _assemblyPath;
  1579. private readonly string _assemblyName;
  1580. public Helper (string assemblyPath, string assemblyName)
  1581. {
  1582. _assemblyPath = assemblyPath;
  1583. _assemblyName = assemblyName;
  1584. }
  1585. public void Test ()
  1586. {
  1587. AssemblyName assemblyName = AssemblyName.GetAssemblyName (_assemblyPath);
  1588. Assert.AreEqual (_assemblyName, assemblyName.ToString ());
  1589. }
  1590. }
  1591. [Test]//Bug #7126
  1592. public void CannotCreateInstanceOfSaveOnlyAssembly ()
  1593. {
  1594. var asm_builder = genAssembly (AssemblyBuilderAccess.Save);
  1595. var mod_builder = asm_builder.DefineDynamicModule("Foo", "Foo.dll");
  1596. var type_builder = mod_builder.DefineType("Foo",
  1597. TypeAttributes.Public | TypeAttributes.Sealed |
  1598. TypeAttributes.Class | TypeAttributes.BeforeFieldInit);
  1599. var type = type_builder.CreateType();
  1600. try {
  1601. Activator.CreateInstance(type);
  1602. Assert.Fail ("Cannot create instance of save only type");
  1603. } catch (NotSupportedException e) {
  1604. }
  1605. }
  1606. }
  1607. }