2004-03-09 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
1 //
2 // System.Reflection.Emit/AssemblyBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Resources;
13 using System.IO;
14 using System.Security.Policy;
15 using System.Runtime.Serialization;
16 using System.Globalization;
17 using System.Runtime.CompilerServices;
18 using System.Collections;
19 using System.Runtime.InteropServices;
20 using System.Security.Cryptography;
21 using System.Security.Permissions;
22
23 using Mono.Security;
24 using Mono.Security.Cryptography;
25
26 namespace System.Reflection.Emit {
27
28         internal struct RefEmitPermissionSet {
29                 public SecurityAction action;
30                 public string pset;
31
32                 public RefEmitPermissionSet (SecurityAction action, string pset) {
33                         this.action = action;
34                         this.pset = pset;
35                 }
36         }
37
38         internal struct MonoResource {
39                 public byte[] data;
40                 public string name;
41                 public string filename;
42                 public ResourceAttributes attrs;
43                 public int offset;
44         }
45
46         internal struct MonoWin32Resource {
47                 public int res_type;
48                 public int res_id;
49                 public int lang_id;
50                 public byte[] data;
51
52                 public MonoWin32Resource (int res_type, int res_id, int lang_id, byte[] data) {
53                         this.res_type = res_type;
54                         this.res_id = res_id;
55                         this.lang_id = lang_id;
56                         this.data = data;
57                 }
58         }
59
60         public sealed class AssemblyBuilder : Assembly {
61                 #region Sync with reflection.h
62                 private IntPtr dynamic_assembly;
63                 private MethodInfo entry_point;
64                 private ModuleBuilder[] modules;
65                 private string name;
66                 private string dir;
67                 private CustomAttributeBuilder[] cattrs;
68                 private MonoResource[] resources;
69                 byte[] public_key;
70                 string version;
71                 string culture;
72                 uint algid;
73                 uint flags;
74                 PEFileKinds pekind = PEFileKinds.Dll;
75                 bool delay_sign;
76                 uint access;
77                 Module[] loaded_modules;
78                 MonoWin32Resource[] win32_resources;
79                 #endregion
80                 internal Type corlib_object_type = typeof (System.Object);
81                 internal Type corlib_value_type = typeof (System.ValueType);
82                 internal Type corlib_enum_type = typeof (System.Enum);
83                 internal Type corlib_void_type = typeof (void);
84                 ArrayList resource_writers = null;
85                 Win32VersionResource version_res;
86                 bool created;
87                 bool is_module_only;
88                 string keyfile_name;
89                 private Mono.Security.StrongName sn;
90
91                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
92                 private static extern void basic_init (AssemblyBuilder ab);
93                 
94                 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
95                         name = n.Name;
96                         if (directory == null || directory == String.Empty)
97                                 dir = Directory.GetCurrentDirectory ();
98                         else
99                                 dir = directory;
100                         this.access = (uint)access;
101
102                         /* Set defaults from n */
103                         if (n.CultureInfo != null) {
104                                 culture = n.CultureInfo.Name;
105                         }
106                         Version v = n.Version;
107                         if (v != null) {
108                                 version = v.ToString ();
109                         }
110
111                         basic_init (this);
112                 }
113
114                 public override string CodeBase {
115                         get {
116                                 throw not_supported ();
117                         }
118                 }
119                 
120                 public override MethodInfo EntryPoint {
121                         get {
122                                 return entry_point;
123                         }
124                 }
125
126                 public override string Location {
127                         get {
128                                 throw not_supported ();
129                         }
130                 }
131
132 #if NET_1_1
133                 /* This is to keep signature compatibility with MS.NET */
134                 public override string ImageRuntimeVersion {
135                         get {
136                                 return base.ImageRuntimeVersion;
137                         }
138                 }
139 #endif
140
141                 public void AddResourceFile (string name, string fileName)
142                 {
143                         AddResourceFile (name, fileName, ResourceAttributes.Public);
144                 }
145
146                 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
147                 {
148                         AddResourceFile (name, fileName, attribute, true);
149                 }
150
151                 private void AddResourceFile (string name, string fileName, ResourceAttributes attribute, bool fileNeedsToExists)
152                 {
153                         check_name_and_filename (name, fileName, fileNeedsToExists);
154
155                         // Resource files are created/searched under the assembly storage
156                         // directory
157                         if (dir != null)
158                                 fileName = Path.Combine (dir, fileName);
159
160                         if (resources != null) {
161                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
162                                 System.Array.Copy(resources, new_r, resources.Length);
163                                 resources = new_r;
164                         } else {
165                                 resources = new MonoResource [1];
166                         }
167                         int p = resources.Length - 1;
168                         resources [p].name = name;
169                         resources [p].filename = fileName;
170                         resources [p].attrs = attribute;
171                 }
172
173                 public void EmbedResourceFile (string name, string fileName)
174                 {
175                         EmbedResourceFile (name, fileName, ResourceAttributes.Public);
176                 }
177
178                 public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
179                 {
180                         if (resources != null) {
181                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
182                                 System.Array.Copy(resources, new_r, resources.Length);
183                                 resources = new_r;
184                         } else {
185                                 resources = new MonoResource [1];
186                         }
187                         int p = resources.Length - 1;
188                         resources [p].name = name;
189                         resources [p].attrs = attribute;
190                         try {
191                                 FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
192                                 long len = s.Length;
193                                 resources [p].data = new byte [len];
194                                 s.Read (resources [p].data, 0, (int)len);
195                                 s.Close ();
196                         } catch {
197                                 /* do something */
198                         }
199                 }
200
201                 internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute)
202                 {
203                         if (resources != null) {
204                                 MonoResource[] new_r = new MonoResource [resources.Length + 1];
205                                 System.Array.Copy(resources, new_r, resources.Length);
206                                 resources = new_r;
207                         } else {
208                                 resources = new MonoResource [1];
209                         }
210                         int p = resources.Length - 1;
211                         resources [p].name = name;
212                         resources [p].attrs = attribute;
213                         resources [p].data = blob;
214                 }
215
216                 public ModuleBuilder DefineDynamicModule (string name)
217                 {
218                         return DefineDynamicModule (name, name, false, true);
219                 }
220
221                 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
222                 {
223                         return DefineDynamicModule (name, name, emitSymbolInfo, true);
224                 }
225
226                 public ModuleBuilder DefineDynamicModule(string name, string fileName)
227                 {
228                         return DefineDynamicModule (name, fileName, false, false);
229                 }
230
231                 public ModuleBuilder DefineDynamicModule (string name, string fileName,
232                                                           bool emitSymbolInfo)
233                 {
234                         return DefineDynamicModule (name, fileName, emitSymbolInfo, false);
235                 }
236
237                 private ModuleBuilder DefineDynamicModule (string name, string fileName,
238                                                                                                    bool emitSymbolInfo, bool transient)
239                 {
240                         check_name_and_filename (name, fileName, false);
241
242                         if (!transient) {
243                                 if (Path.GetExtension (fileName) == String.Empty)
244                                         throw new ArgumentException ("Module file name '" + fileName + "' must have file extension.");
245                                 if (!IsSave)
246                                         throw new NotSupportedException ("Persistable modules are not supported in a dynamic assembly created with AssemblyBuilderAccess.Run");
247                                 if (created)
248                                         throw new InvalidOperationException ("Assembly was already saved.");
249                         }
250
251                         ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, transient);
252
253                         if ((modules != null) && is_module_only)
254                                 throw new InvalidOperationException ("A module-only assembly can only contain one module.");
255
256                         if (modules != null) {
257                                 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
258                                 System.Array.Copy(modules, new_modules, modules.Length);
259                                 modules = new_modules;
260                         } else {
261                                 modules = new ModuleBuilder [1];
262                         }
263                         modules [modules.Length - 1] = r;
264                         return r;
265                 }
266
267                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
268                 private extern Module InternalAddModule (string fileName);
269
270                 /*
271                  * Mono extension to support /addmodule in mcs.
272                  */
273                 internal Module AddModule (string fileName)
274                 {
275                         if (fileName == null)
276                                 throw new ArgumentNullException (fileName);
277
278                         Module m = InternalAddModule (fileName);
279
280                         if (loaded_modules != null) {
281                                 Module[] new_modules = new Module [loaded_modules.Length + 1];
282                                 System.Array.Copy (loaded_modules, new_modules, loaded_modules.Length);
283                                 loaded_modules = new_modules;
284                         } else {
285                                 loaded_modules = new Module [1];
286                         }
287                         loaded_modules [loaded_modules.Length - 1] = m;
288
289                         return m;
290                 }
291
292                 public IResourceWriter DefineResource (string name, string description, string fileName)
293                 {
294                         return DefineResource (name, description, fileName, ResourceAttributes.Public);
295                 }
296
297                 public IResourceWriter DefineResource (string name, string description,
298                                                        string fileName, ResourceAttributes attribute)
299                 {
300                         IResourceWriter writer;
301
302                         // description seems to be ignored
303                         AddResourceFile (name, fileName, attribute, false);
304                         writer = new ResourceWriter (fileName);
305                         if (resource_writers == null)
306                                 resource_writers = new ArrayList ();
307                         resource_writers.Add (writer);
308                         return writer;
309                 }
310
311                 private void AddUnmanagedResource (Win32Resource res) {
312                         MemoryStream ms = new MemoryStream ();
313                         res.WriteTo (ms);
314
315                         if (win32_resources != null) {
316                                 MonoWin32Resource[] new_res = new MonoWin32Resource [win32_resources.Length + 1];
317                                 System.Array.Copy (win32_resources, new_res, win32_resources.Length);
318                                 win32_resources = new_res;
319                         }
320                         else
321                                 win32_resources = new MonoWin32Resource [1];
322
323                         win32_resources [win32_resources.Length - 1] = new MonoWin32Resource (res.Type.Id, res.Name.Id, res.Language, ms.ToArray ());
324                 }
325
326                 [MonoTODO]
327                 public void DefineUnmanagedResource (byte[] resource)
328                 {
329                         if (resource == null)
330                                 throw new ArgumentNullException ("resource");
331
332                         /*
333                          * The format of the argument byte array is not documented
334                          * so this method is impossible to implement.
335                          */
336
337                         throw new NotImplementedException ();
338                 }
339
340                 public void DefineUnmanagedResource (string resourceFileName)
341                 {
342                         if (resourceFileName == null)
343                                 throw new ArgumentNullException ("resourceFileName");
344                         if (resourceFileName == String.Empty)
345                                 throw new ArgumentException ("resourceFileName");
346                         if (!File.Exists (resourceFileName) || Directory.Exists (resourceFileName))
347                                 throw new FileNotFoundException ("File '" + resourceFileName + "' does not exists or is a directory.");
348
349                         using (FileStream fs = new FileStream (resourceFileName, FileMode.Open)) {
350                                 Win32ResFileReader reader = new Win32ResFileReader (fs);
351
352                                 foreach (Win32EncodedResource res in reader.ReadResources ()) {
353                                         if (res.Name.IsName || res.Type.IsName)
354                                                 throw new InvalidOperationException ("resource files with named resources or non-default resource types are not supported.");
355
356                                         AddUnmanagedResource (res);
357                                 }
358                         }
359                 }
360
361                 public void DefineVersionInfoResource ()
362                 {
363                         if (version_res != null)
364                                 throw new ArgumentException ("Native resource has already been defined.");                      
365
366                         version_res = new Win32VersionResource (1, 0);
367
368                         if (cattrs != null) {
369                                 foreach (CustomAttributeBuilder cb in cattrs) {
370                                         string attrname = cb.Ctor.ReflectedType.FullName;
371
372                                         if (attrname == "System.Reflection.AssemblyProductAttribute")
373                                                 version_res.ProductName = cb.string_arg ();
374                                         else if (attrname == "System.Reflection.AssemblyCompanyAttribute")
375                                                 version_res.CompanyName = cb.string_arg ();
376                                         else if (attrname == "System.Reflection.AssemblyCopyrightAttribute")
377                                                 version_res.LegalCopyright = cb.string_arg ();
378                                         else if (attrname == "System.Reflection.AssemblyTrademarkAttribute")
379                                                 version_res.LegalTrademarks = cb.string_arg ();
380                                         else if (attrname == "System.Reflection.AssemblyCultureAttribute")
381                                                 version_res.FileLanguage = new CultureInfo (cb.string_arg ()).LCID;
382                                         else if (attrname == "System.Reflection.AssemblyFileVersionAttribute")
383                                                 version_res.FileVersion = cb.string_arg ();
384                                         else if (attrname == "System.Reflection.AssemblyInformationalVersionAttribute")
385                                                 version_res.ProductVersion = cb.string_arg ();
386                                         else if (attrname == "System.Reflection.AssemblyTitleAttribute")
387                                                 version_res.FileDescription = cb.string_arg ();
388                                         else if (attrname == "System.Reflection.AssemblyDescriptionAttribute")
389                                                 version_res.Comments = cb.string_arg ();
390                                 }
391                         }
392                 }
393
394                 public void DefineVersionInfoResource (string product, string productVersion,
395                                                        string company, string copyright, string trademark)
396                 {
397                         if (version_res != null)
398                                 throw new ArgumentException ("Native resource has already been defined.");
399
400                         /*
401                          * We can only create the resource later, when the file name and
402                          * the binary version is known.
403                          */
404
405                         version_res = new Win32VersionResource (1, 0);
406                         version_res.ProductName = product;
407                         version_res.ProductVersion = productVersion;
408                         version_res.CompanyName = company;
409                         version_res.LegalCopyright = copyright;
410                         version_res.LegalTrademarks = trademark;
411                 }
412
413                 /* 
414                  * Mono extension to support /win32icon in mcs
415                  */
416                 internal void DefineIconResource (string iconFileName)
417                 {
418                         if (iconFileName == null)
419                                 throw new ArgumentNullException ("iconFileName");
420                         if (iconFileName == String.Empty)
421                                 throw new ArgumentException ("iconFileName");
422                         if (!File.Exists (iconFileName) || Directory.Exists (iconFileName))
423                                 throw new FileNotFoundException ("File '" + iconFileName + "' does not exists or is a directory.");
424
425                         using (FileStream fs = new FileStream (iconFileName, FileMode.Open)) {
426                                 Win32IconFileReader reader = new Win32IconFileReader (fs);
427                                 
428                                 ICONDIRENTRY[] entries = reader.ReadIcons ();
429
430                                 Win32IconResource[] icons = new Win32IconResource [entries.Length];
431                                 for (int i = 0; i < entries.Length; ++i) {
432                                         icons [i] = new Win32IconResource (i + 1, 0, entries [i]);
433                                         AddUnmanagedResource (icons [i]);
434                                 }
435
436                                 Win32GroupIconResource group = new Win32GroupIconResource (1, 0, icons);
437                                 AddUnmanagedResource (group);
438                         }
439                 }
440
441                 private void DefineVersionInfoResourceImpl (string fileName) {
442                         // Add missing info
443                         if (version_res.FileVersion == "0.0.0.0")
444                                 version_res.FileVersion = version;
445                         version_res.InternalName = Path.GetFileNameWithoutExtension (fileName);
446                         version_res.OriginalFilename = fileName;
447
448                         AddUnmanagedResource (version_res);
449                 }
450
451                 public ModuleBuilder GetDynamicModule (string name)
452                 {
453                         if (name == null)
454                                 throw new ArgumentNullException ("name");
455                         if (name == "")
456                                 throw new ArgumentException ("Name can't be null");
457
458                         if (modules != null)
459                                 for (int i = 0; i < modules.Length; ++i)
460                                         if (modules [i].name == name)
461                                                 return modules [i];
462                         return null;
463                 }
464
465                 public override Type[] GetExportedTypes ()
466                 {
467                         throw not_supported ();
468                 }
469
470                 public override FileStream GetFile (string name)
471                 {
472                         throw not_supported ();
473                 }
474
475                 public override FileStream[] GetFiles() {
476                         throw not_supported ();
477                 }
478                 
479                 public override FileStream[] GetFiles(bool getResourceModules) {
480                         throw not_supported ();
481                 }
482
483                 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) {
484                         throw not_supported ();
485                 }
486
487                 public override string[] GetManifestResourceNames() {
488                         throw not_supported ();
489                 }
490
491                 public override Stream GetManifestResourceStream(string name) {
492                         throw not_supported ();
493                 }
494                 public override Stream GetManifestResourceStream(Type type, string name) {
495                         throw not_supported ();
496                 }
497
498                 internal bool IsSave {
499                         get {
500                                 return access != (uint)AssemblyBuilderAccess.Run;
501                         }
502                 }
503
504                 internal string AssemblyDir {
505                         get {
506                                 return dir;
507                         }
508                 }
509
510                 /*
511                  * Mono extension. If this is set, the assembly can only contain one
512                  * module, access should be Save, and the saved image will not contain an
513                  * assembly manifest.
514                  */
515                 internal bool IsModuleOnly {
516                         get {
517                                 return is_module_only;
518                         }
519                         set {
520                                 is_module_only = value;
521                         }
522                 }
523
524                 public void Save (string assemblyFileName)
525                 {
526                         if (resource_writers != null) {
527                                 foreach (IResourceWriter writer in resource_writers) {
528                                         writer.Generate ();
529                                         writer.Close ();
530                                 }
531                         }
532
533                         // Create a main module if not already created
534                         ModuleBuilder mainModule = null;
535                         if (modules != null) {
536                                 foreach (ModuleBuilder module in modules)
537                                         if (module.FullyQualifiedName == assemblyFileName)
538                                                 mainModule = module;
539                         }
540                         if (mainModule == null)
541                                 mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
542
543                         if (!is_module_only)
544                                 mainModule.IsMain = true;
545
546                         /* 
547                          * Create a new entry point if the one specified
548                          * by the user is in another module.
549                          */
550                         if ((entry_point != null) && entry_point.DeclaringType.Module != mainModule) {
551                                 Type[] paramTypes;
552                                 if (entry_point.GetParameters ().Length == 1)
553                                         paramTypes = new Type [] { typeof (string) };
554                                 else
555                                         paramTypes = new Type [0];
556
557                                 MethodBuilder mb = mainModule.DefineGlobalMethod ("__EntryPoint$", MethodAttributes.Static|MethodAttributes.PrivateScope, entry_point.ReturnType, paramTypes);
558                                 ILGenerator ilgen = mb.GetILGenerator ();
559                                 if (paramTypes.Length == 1)
560                                         ilgen.Emit (OpCodes.Ldarg_0);
561                                 ilgen.Emit (OpCodes.Tailcall);
562                                 ilgen.Emit (OpCodes.Call, entry_point);
563                                 ilgen.Emit (OpCodes.Ret);
564
565                                 entry_point = mb;
566                         }
567
568                         if (version_res != null)
569                                 DefineVersionInfoResourceImpl (assemblyFileName);
570
571                         if ((keyfile_name != null) && (keyfile_name != String.Empty)) {
572                                 // the StrongName key file may be relative to (a) the compiled
573                                 // file or (b) to the output assembly. See bugzilla #55320
574                                 // http://bugzilla.ximian.com/show_bug.cgi?id=55320
575
576                                 // (a) relative to the compiled file
577                                 string filename = Path.GetFullPath (keyfile_name);
578                                 bool exist = File.Exists (filename);
579                                 if ((!exist) && (AssemblyDir != null)) {
580                                         // (b) relative to the outputed assembly
581                                         filename = Path.GetFullPath (Path.Combine (AssemblyDir, keyfile_name));
582                                         exist = File.Exists (filename);
583                                 }
584
585                                 if (exist) {
586                                         using (FileStream fs = new FileStream (filename, FileMode.Open)) {
587                                                 byte[] snkeypair = new byte [fs.Length];
588                                                 fs.Read (snkeypair, 0, snkeypair.Length);
589
590                                                 // this will import public or private/public keys
591                                                 RSA rsa = CryptoConvert.FromCapiKeyBlob (snkeypair);
592                                                 // and export only the public part
593                                                 sn = new Mono.Security.StrongName (rsa);
594                                                 public_key = sn.PublicKey;
595                                         }
596                                 }
597                         }
598                         
599                         foreach (ModuleBuilder module in modules)
600                                 if (module != mainModule)
601                                         module.Save ();
602
603                         // Write out the main module at the end, because it needs to
604                         // contain the hash of the other modules
605                         mainModule.Save ();
606
607                         // if not delayed then we directly strongname the assembly
608                         if ((sn != null) && (!delay_sign)) {
609                                 sn.Sign (System.IO.Path.Combine (this.AssemblyDir, assemblyFileName));
610                         }
611
612                         created = true;
613                 }
614
615                 public void SetEntryPoint (MethodInfo entryMethod)
616                 {
617                         SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
618                 }
619
620                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
621                 {
622                         if (entryMethod == null)
623                                 throw new ArgumentNullException ("entryMethod");
624                         if (entryMethod.DeclaringType.Assembly != this)
625                                 throw new InvalidOperationException ("Entry method is not defined in the same assembly.");
626
627                         entry_point = entryMethod;
628                         pekind = fileKind;
629                 }
630
631                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) 
632                 {
633                         if (customBuilder == null)
634                                 throw new ArgumentNullException ("customBuilder");
635
636                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
637                         byte[] data;
638                         int len, pos;
639
640                         if (attrname == "System.Reflection.AssemblyVersionAttribute") {
641                                 version = create_assembly_version (customBuilder.string_arg ());
642                                 return;
643                         } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
644                                 keyfile_name = customBuilder.string_arg ();
645                                 return;
646                         } else if (attrname == "System.Reflection.AssemblyKeyNameAttribute") {
647                                 string key_name = customBuilder.string_arg ();
648                                 if (key_name == String.Empty)
649                                         return;
650                                 CspParameters csparam = new CspParameters ();
651                                 csparam.KeyContainerName = key_name;
652                                 RSA rsacsp = new RSACryptoServiceProvider (csparam);
653                                 sn = new Mono.Security.StrongName (rsacsp);
654                                 public_key = sn.PublicKey;
655                                 return;
656                         } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
657                                 culture = customBuilder.string_arg ();
658                         } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
659                                 data = customBuilder.Data;
660                                 pos = 2;
661                                 algid = (uint)data [pos];
662                                 algid |= ((uint)data [pos + 1]) << 8;
663                                 algid |= ((uint)data [pos + 2]) << 16;
664                                 algid |= ((uint)data [pos + 3]) << 24;
665                         } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
666                                 data = customBuilder.Data;
667                                 pos = 2;
668                                 flags = (uint)data [pos];
669                                 flags |= ((uint)data [pos + 1]) << 8;
670                                 flags |= ((uint)data [pos + 2]) << 16;
671                                 flags |= ((uint)data [pos + 3]) << 24;
672                                 return;
673                         } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
674                                 data = customBuilder.Data;
675                                 pos = 2;
676                                 delay_sign = data [2] != 0;
677                         }
678                         if (cattrs != null) {
679                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
680                                 cattrs.CopyTo (new_array, 0);
681                                 new_array [cattrs.Length] = customBuilder;
682                                 cattrs = new_array;
683                         } else {
684                                 cattrs = new CustomAttributeBuilder [1];
685                                 cattrs [0] = customBuilder;
686                         }
687                 }
688                 public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
689                         if (con == null)
690                                 throw new ArgumentNullException ("con");
691                         if (binaryAttribute == null)
692                                 throw new ArgumentNullException ("binaryAttribute");
693
694                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
695                 }
696
697                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
698                         this.corlib_object_type = corlib_object_type;
699                         this.corlib_value_type = corlib_value_type;
700                         this.corlib_enum_type = corlib_enum_type;
701                 }
702
703                 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
704                 {
705                         SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
706                         this.corlib_void_type = corlib_void_type;
707                 }
708
709                 private Exception not_supported () {
710                         // Strange message but this is what MS.NET prints...
711                         return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
712                 }
713
714                 private void check_name_and_filename (string name, string fileName,
715                                                                                           bool fileNeedsToExists) {
716                         if (name == null)
717                                 throw new ArgumentNullException ("name");
718                         if (fileName == null)
719                                 throw new ArgumentNullException ("fileName");
720                         if (name == "")
721                                 throw new ArgumentException ("name cannot be empty", "name");
722                         if (fileName == "")
723                                 throw new ArgumentException ("fileName cannot be empty", "fileName");
724                         if (Path.GetFileName (fileName) != fileName)
725                                 throw new ArgumentException ("fileName '" + fileName + "' must not include a path.");
726
727                         // Resource files are created/searched under the assembly storage
728                         // directory
729                         string fullFileName = fileName;
730                         if (dir != null)
731                                 fullFileName = Path.Combine (dir, fileName);
732
733                         if (fileNeedsToExists && !File.Exists (fullFileName))
734                                 throw new FileNotFoundException ("Could not find file '" + fileName + "'");
735
736                         if (resources != null) {
737                                 for (int i = 0; i < resources.Length; ++i) {
738                                         if (resources [i].filename == fullFileName)
739                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
740                                         if (resources [i].name == name)
741                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
742                                 }
743                         }
744
745                         if (modules != null) {
746                                 for (int i = 0; i < modules.Length; ++i) {
747                                         // Use fileName instead of fullFileName here
748                                         if (!modules [i].IsTransient () && (modules [i].FileName == fileName))
749                                                 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
750                                         if (modules [i].Name == name)
751                                                 throw new ArgumentException ("Duplicate name '" + name + "'");
752                                 }
753                         }
754                 }
755
756                 private String create_assembly_version (String version) {
757                         String[] parts = version.Split ('.');
758                         int[] ver = new int [4] { 0, 0, 0, 0 };
759
760                         if ((parts.Length < 0) || (parts.Length > 4))
761                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
762
763                         for (int i = 0; i < parts.Length; ++i) {
764                                 if (parts [i] == "*") {
765                                         DateTime now = DateTime.Now;
766
767                                         if (i == 2) {
768                                                 ver [2] = (now - new DateTime (2000, 1, 1)).Days;
769                                                 if (parts.Length == 3)
770                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
771                                         }
772                                         else
773                                                 if (i == 3)
774                                                         ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
775                                         else
776                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
777                                 }
778                                 else {
779                                         try {
780                                                 ver [i] = Int32.Parse (parts [i]);
781                                         }
782                                         catch (FormatException) {
783                                                 throw new ArgumentException ("The version specified '" + version + "' is invalid");
784                                         }
785                                 }
786                         }
787
788                         return ver [0] + "." + ver [1] + "." + ver [2] + "." + ver [3];
789                 }
790         }
791 }