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