2 // System.Reflection.Emit/AssemblyBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Reflection;
35 using System.Resources;
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;
44 using System.Security.Cryptography;
45 using System.Security.Permissions;
48 using Mono.Security.Cryptography;
50 namespace System.Reflection.Emit
52 internal enum NativeResourceType
60 internal struct RefEmitPermissionSet {
61 public SecurityAction action;
64 public RefEmitPermissionSet (SecurityAction action, string pset) {
70 internal struct MonoResource {
73 public string filename;
74 public ResourceAttributes attrs;
79 internal struct MonoWin32Resource {
85 public MonoWin32Resource (int res_type, int res_id, int lang_id, byte[] data) {
86 this.res_type = res_type;
88 this.lang_id = lang_id;
95 [ComDefaultInterface (typeof (_AssemblyBuilder))]
97 [ClassInterface (ClassInterfaceType.None)]
98 public sealed class AssemblyBuilder : Assembly, _AssemblyBuilder {
99 #region Sync with object-internals.h
100 private UIntPtr dynamic_assembly; /* GC-tracked */
101 private MethodInfo entry_point;
102 private ModuleBuilder[] modules;
105 private CustomAttributeBuilder[] cattrs;
106 private MonoResource[] resources;
112 PEFileKinds pekind = PEFileKinds.Dll;
115 Module[] loaded_modules;
116 MonoWin32Resource[] win32_resources;
117 private RefEmitPermissionSet[] permissions_minimum;
118 private RefEmitPermissionSet[] permissions_optional;
119 private RefEmitPermissionSet[] permissions_refused;
120 PortableExecutableKinds peKind;
121 ImageFileMachine machine;
122 bool corlib_internal;
123 Type[] type_forwarders;
125 internal Type corlib_object_type = typeof (System.Object);
126 internal Type corlib_value_type = typeof (System.ValueType);
127 internal Type corlib_enum_type = typeof (System.Enum);
128 internal Type corlib_void_type = typeof (void);
129 ArrayList resource_writers = null;
130 Win32VersionResource version_res;
133 private Mono.Security.StrongName sn;
134 NativeResourceType native_resource;
135 readonly bool is_compiler_context;
136 string versioninfo_culture;
138 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139 private static extern void basic_init (AssemblyBuilder ab);
141 /* Keep this in sync with codegen.cs in mcs */
142 private const AssemblyBuilderAccess COMPILER_ACCESS = (AssemblyBuilderAccess) 0x800;
144 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access, bool corlib_internal)
146 #if BOOTSTRAP_WITH_OLDLIB
147 is_compiler_context = true;
149 is_compiler_context = (access & COMPILER_ACCESS) != 0;
152 // remove Mono specific flag to allow enum check to pass
153 access &= ~COMPILER_ACCESS;
156 if (!Enum.IsDefined (typeof (AssemblyBuilderAccess), access))
157 throw new ArgumentException (string.Format (CultureInfo.InvariantCulture,
158 "Argument value {0} is not valid.", (int) access),
163 this.access = (uint)access;
164 flags = (uint) n.Flags;
166 // don't call GetCurrentDirectory for Run-only builders (CAS may not like that)
167 if (IsSave && (directory == null || directory.Length == 0)) {
168 dir = Directory.GetCurrentDirectory ();
173 /* Set defaults from n */
174 if (n.CultureInfo != null) {
175 culture = n.CultureInfo.Name;
176 versioninfo_culture = n.CultureInfo.Name;
178 Version v = n.Version;
180 version = v.ToString ();
183 if (n.KeyPair != null) {
184 // full keypair is available (for signing)
185 sn = n.KeyPair.StrongName ();
188 // public key is available (for delay-signing)
189 byte[] pk = n.GetPublicKey ();
190 if ((pk != null) && (pk.Length > 0)) {
191 sn = new Mono.Security.StrongName (pk);
195 this.corlib_internal = corlib_internal;
200 public override string CodeBase {
202 throw not_supported ();
206 public override MethodInfo EntryPoint {
212 public override string Location {
214 throw not_supported ();
219 /* This is to keep signature compatibility with MS.NET */
220 public override string ImageRuntimeVersion {
222 return base.ImageRuntimeVersion;
229 public override bool ReflectionOnly {
230 get { return base.ReflectionOnly; }
234 public void AddResourceFile (string name, string fileName)
236 AddResourceFile (name, fileName, ResourceAttributes.Public);
239 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
241 AddResourceFile (name, fileName, attribute, true);
244 private void AddResourceFile (string name, string fileName, ResourceAttributes attribute, bool fileNeedsToExists)
246 check_name_and_filename (name, fileName, fileNeedsToExists);
248 // Resource files are created/searched under the assembly storage
251 fileName = Path.Combine (dir, fileName);
253 if (resources != null) {
254 MonoResource[] new_r = new MonoResource [resources.Length + 1];
255 System.Array.Copy(resources, new_r, resources.Length);
258 resources = new MonoResource [1];
260 int p = resources.Length - 1;
261 resources [p].name = name;
262 resources [p].filename = fileName;
263 resources [p].attrs = attribute;
267 /// Don't change the method name and parameters order. It is used by mcs
269 internal void AddPermissionRequests (PermissionSet required, PermissionSet optional, PermissionSet refused)
272 throw new InvalidOperationException ("Assembly was already saved.");
274 // required for base Assembly class (so the permissions
275 // can be used even if the assembly isn't saved to disk)
277 _optional = optional;
280 // required to reuse AddDeclarativeSecurity support
281 // already present in the runtime
282 if (required != null) {
283 permissions_minimum = new RefEmitPermissionSet [1];
284 permissions_minimum [0] = new RefEmitPermissionSet (
285 SecurityAction.RequestMinimum, required.ToXml ().ToString ());
287 if (optional != null) {
288 permissions_optional = new RefEmitPermissionSet [1];
289 permissions_optional [0] = new RefEmitPermissionSet (
290 SecurityAction.RequestOptional, optional.ToXml ().ToString ());
292 if (refused != null) {
293 permissions_refused = new RefEmitPermissionSet [1];
294 permissions_refused [0] = new RefEmitPermissionSet (
295 SecurityAction.RequestRefuse, refused.ToXml ().ToString ());
299 internal void EmbedResourceFile (string name, string fileName)
301 EmbedResourceFile (name, fileName, ResourceAttributes.Public);
304 internal void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
306 if (resources != null) {
307 MonoResource[] new_r = new MonoResource [resources.Length + 1];
308 System.Array.Copy(resources, new_r, resources.Length);
311 resources = new MonoResource [1];
313 int p = resources.Length - 1;
314 resources [p].name = name;
315 resources [p].attrs = attribute;
317 FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
319 resources [p].data = new byte [len];
320 s.Read (resources [p].data, 0, (int)len);
327 internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute)
329 if (resources != null) {
330 MonoResource[] new_r = new MonoResource [resources.Length + 1];
331 System.Array.Copy(resources, new_r, resources.Length);
334 resources = new MonoResource [1];
336 int p = resources.Length - 1;
337 resources [p].name = name;
338 resources [p].attrs = attribute;
339 resources [p].data = blob;
343 internal void AddTypeForwarder (Type t) {
345 throw new ArgumentNullException ("t");
347 if (type_forwarders == null) {
348 type_forwarders = new Type [1] { t };
350 Type[] arr = new Type [type_forwarders.Length + 1];
351 Array.Copy (type_forwarders, arr, type_forwarders.Length);
352 type_forwarders = arr;
357 public ModuleBuilder DefineDynamicModule (string name)
359 return DefineDynamicModule (name, name, false, true);
362 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
364 return DefineDynamicModule (name, name, emitSymbolInfo, true);
367 public ModuleBuilder DefineDynamicModule(string name, string fileName)
369 return DefineDynamicModule (name, fileName, false, false);
372 public ModuleBuilder DefineDynamicModule (string name, string fileName,
375 return DefineDynamicModule (name, fileName, emitSymbolInfo, false);
378 private ModuleBuilder DefineDynamicModule (string name, string fileName, bool emitSymbolInfo, bool transient)
380 check_name_and_filename (name, fileName, false);
383 if (Path.GetExtension (fileName) == String.Empty)
384 throw new ArgumentException ("Module file name '" + fileName + "' must have file extension.");
386 throw new NotSupportedException ("Persistable modules are not supported in a dynamic assembly created with AssemblyBuilderAccess.Run");
388 throw new InvalidOperationException ("Assembly was already saved.");
391 ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, transient);
393 if ((modules != null) && is_module_only)
394 throw new InvalidOperationException ("A module-only assembly can only contain one module.");
396 if (modules != null) {
397 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
398 System.Array.Copy(modules, new_modules, modules.Length);
399 modules = new_modules;
401 modules = new ModuleBuilder [1];
403 modules [modules.Length - 1] = r;
407 [MethodImplAttribute(MethodImplOptions.InternalCall)]
408 private extern Module InternalAddModule (string fileName);
411 * Mono extension to support /addmodule in mcs.
413 internal Module AddModule (string fileName)
415 if (fileName == null)
416 throw new ArgumentNullException (fileName);
418 Module m = InternalAddModule (fileName);
420 if (loaded_modules != null) {
421 Module[] new_modules = new Module [loaded_modules.Length + 1];
422 System.Array.Copy (loaded_modules, new_modules, loaded_modules.Length);
423 loaded_modules = new_modules;
425 loaded_modules = new Module [1];
427 loaded_modules [loaded_modules.Length - 1] = m;
432 public IResourceWriter DefineResource (string name, string description, string fileName)
434 return DefineResource (name, description, fileName, ResourceAttributes.Public);
437 public IResourceWriter DefineResource (string name, string description,
438 string fileName, ResourceAttributes attribute)
440 IResourceWriter writer;
442 // description seems to be ignored
443 AddResourceFile (name, fileName, attribute, false);
444 writer = new ResourceWriter (fileName);
445 if (resource_writers == null)
446 resource_writers = new ArrayList ();
447 resource_writers.Add (writer);
451 private void AddUnmanagedResource (Win32Resource res) {
452 MemoryStream ms = new MemoryStream ();
455 if (win32_resources != null) {
456 MonoWin32Resource[] new_res = new MonoWin32Resource [win32_resources.Length + 1];
457 System.Array.Copy (win32_resources, new_res, win32_resources.Length);
458 win32_resources = new_res;
461 win32_resources = new MonoWin32Resource [1];
463 win32_resources [win32_resources.Length - 1] = new MonoWin32Resource (res.Type.Id, res.Name.Id, res.Language, ms.ToArray ());
466 [MonoTODO ("Not currently implemenented")]
467 public void DefineUnmanagedResource (byte[] resource)
469 if (resource == null)
470 throw new ArgumentNullException ("resource");
471 if (native_resource != NativeResourceType.None)
472 throw new ArgumentException ("Native resource has already been defined.");
474 // avoid definition of more than one unmanaged resource
475 native_resource = NativeResourceType.Unmanaged;
478 * The format of the argument byte array is not documented
479 * so this method is impossible to implement.
482 throw new NotImplementedException ();
485 public void DefineUnmanagedResource (string resourceFileName)
487 if (resourceFileName == null)
488 throw new ArgumentNullException ("resourceFileName");
489 if (resourceFileName.Length == 0)
490 throw new ArgumentException ("resourceFileName");
491 if (!File.Exists (resourceFileName) || Directory.Exists (resourceFileName))
492 throw new FileNotFoundException ("File '" + resourceFileName + "' does not exists or is a directory.");
493 if (native_resource != NativeResourceType.None)
494 throw new ArgumentException ("Native resource has already been defined.");
496 // avoid definition of more than one unmanaged resource
497 native_resource = NativeResourceType.Unmanaged;
499 using (FileStream fs = new FileStream (resourceFileName, FileMode.Open, FileAccess.Read)) {
500 Win32ResFileReader reader = new Win32ResFileReader (fs);
502 foreach (Win32EncodedResource res in reader.ReadResources ()) {
503 if (res.Name.IsName || res.Type.IsName)
504 throw new InvalidOperationException ("resource files with named resources or non-default resource types are not supported.");
506 AddUnmanagedResource (res);
511 public void DefineVersionInfoResource ()
513 if (native_resource != NativeResourceType.None)
514 throw new ArgumentException ("Native resource has already been defined.");
516 // avoid definition of more than one unmanaged resource
517 native_resource = NativeResourceType.Assembly;
519 version_res = new Win32VersionResource (1, 0, IsCompilerContext);
522 public void DefineVersionInfoResource (string product, string productVersion,
523 string company, string copyright, string trademark)
525 if (native_resource != NativeResourceType.None)
526 throw new ArgumentException ("Native resource has already been defined.");
528 // avoid definition of more than one unmanaged resource
529 native_resource = NativeResourceType.Explicit;
532 * We can only create the resource later, when the file name and
533 * the binary version is known.
536 version_res = new Win32VersionResource (1, 0, false);
537 version_res.ProductName = product != null ? product : " ";
538 version_res.ProductVersion = productVersion != null ? productVersion : " ";
539 version_res.CompanyName = company != null ? company : " ";
540 version_res.LegalCopyright = copyright != null ? copyright : " ";
541 version_res.LegalTrademarks = trademark != null ? trademark : " ";
545 * Mono extension to support /win32icon in mcs
547 internal void DefineIconResource (string iconFileName)
549 if (iconFileName == null)
550 throw new ArgumentNullException ("iconFileName");
551 if (iconFileName.Length == 0)
552 throw new ArgumentException ("iconFileName");
553 if (!File.Exists (iconFileName) || Directory.Exists (iconFileName))
554 throw new FileNotFoundException ("File '" + iconFileName + "' does not exists or is a directory.");
556 using (FileStream fs = new FileStream (iconFileName, FileMode.Open, FileAccess.Read)) {
557 Win32IconFileReader reader = new Win32IconFileReader (fs);
559 ICONDIRENTRY[] entries = reader.ReadIcons ();
561 Win32IconResource[] icons = new Win32IconResource [entries.Length];
562 for (int i = 0; i < entries.Length; ++i) {
563 icons [i] = new Win32IconResource (i + 1, 0, entries [i]);
564 AddUnmanagedResource (icons [i]);
567 Win32GroupIconResource group = new Win32GroupIconResource (1, 0, icons);
568 AddUnmanagedResource (group);
572 private void DefineVersionInfoResourceImpl (string fileName)
574 if (versioninfo_culture != null)
575 version_res.FileLanguage = new CultureInfo (versioninfo_culture).LCID;
576 version_res.Version = version == null ? "0.0.0.0" : version;
578 if (cattrs != null) {
579 switch (native_resource) {
580 case NativeResourceType.Assembly:
581 foreach (CustomAttributeBuilder cb in cattrs) {
582 string attrname = cb.Ctor.ReflectedType.FullName;
584 if (attrname == "System.Reflection.AssemblyProductAttribute")
585 version_res.ProductName = cb.string_arg ();
586 else if (attrname == "System.Reflection.AssemblyCompanyAttribute")
587 version_res.CompanyName = cb.string_arg ();
588 else if (attrname == "System.Reflection.AssemblyCopyrightAttribute")
589 version_res.LegalCopyright = cb.string_arg ();
590 else if (attrname == "System.Reflection.AssemblyTrademarkAttribute")
591 version_res.LegalTrademarks = cb.string_arg ();
592 else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
593 if (!IsCompilerContext)
594 version_res.FileLanguage = new CultureInfo (cb.string_arg ()).LCID;
595 } else if (attrname == "System.Reflection.AssemblyFileVersionAttribute") {
596 string fileversion = cb.string_arg ();
597 if (!IsCompilerContext || fileversion != null && fileversion.Length != 0)
598 version_res.FileVersion = fileversion;
599 } else if (attrname == "System.Reflection.AssemblyInformationalVersionAttribute")
600 version_res.ProductVersion = cb.string_arg ();
601 else if (attrname == "System.Reflection.AssemblyTitleAttribute")
602 version_res.FileDescription = cb.string_arg ();
603 else if (attrname == "System.Reflection.AssemblyDescriptionAttribute")
604 version_res.Comments = cb.string_arg ();
607 case NativeResourceType.Explicit:
608 foreach (CustomAttributeBuilder cb in cattrs) {
609 string attrname = cb.Ctor.ReflectedType.FullName;
611 if (attrname == "System.Reflection.AssemblyCultureAttribute") {
612 if (!IsCompilerContext)
613 version_res.FileLanguage = new CultureInfo (cb.string_arg ()).LCID;
614 } else if (attrname == "System.Reflection.AssemblyDescriptionAttribute")
615 version_res.Comments = cb.string_arg ();
621 version_res.OriginalFilename = fileName;
623 if (IsCompilerContext) {
624 version_res.InternalName = fileName;
625 if (version_res.ProductVersion.Trim ().Length == 0)
626 version_res.ProductVersion = version_res.FileVersion;
628 version_res.InternalName = Path.GetFileNameWithoutExtension (fileName);
631 AddUnmanagedResource (version_res);
634 public ModuleBuilder GetDynamicModule (string name)
637 throw new ArgumentNullException ("name");
638 if (name.Length == 0)
639 throw new ArgumentException ("Empty name is not legal.", "name");
642 for (int i = 0; i < modules.Length; ++i)
643 if (modules [i].name == name)
648 public override Type[] GetExportedTypes ()
650 throw not_supported ();
653 public override FileStream GetFile (string name)
655 throw not_supported ();
658 public override FileStream[] GetFiles(bool getResourceModules) {
659 throw not_supported ();
662 internal override Module[] GetModulesInternal () {
664 return new Module [0];
666 return (Module[])modules.Clone ();
669 internal override Type[] GetTypes (bool exportedOnly) {
671 if (modules != null) {
672 for (int i = 0; i < modules.Length; ++i) {
673 Type[] types = modules [i].GetTypes ();
677 Type[] tmp = new Type [res.Length + types.Length];
678 Array.Copy (res, 0, tmp, 0, res.Length);
679 Array.Copy (types, 0, tmp, res.Length, types.Length);
683 if (loaded_modules != null) {
684 for (int i = 0; i < loaded_modules.Length; ++i) {
685 Type[] types = loaded_modules [i].GetTypes ();
689 Type[] tmp = new Type [res.Length + types.Length];
690 Array.Copy (res, 0, tmp, 0, res.Length);
691 Array.Copy (types, 0, tmp, res.Length, types.Length);
696 return res == null ? Type.EmptyTypes : res;
699 public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) {
700 throw not_supported ();
703 public override string[] GetManifestResourceNames() {
704 throw not_supported ();
707 public override Stream GetManifestResourceStream(string name) {
708 throw not_supported ();
710 public override Stream GetManifestResourceStream(Type type, string name) {
711 throw not_supported ();
715 * This is set when the the AssemblyBuilder is created by (g)mcs
718 internal bool IsCompilerContext
720 get { return is_compiler_context; }
723 internal bool IsSave {
725 return access != (uint)AssemblyBuilderAccess.Run;
729 internal string AssemblyDir {
736 * Mono extension. If this is set, the assembly can only contain one
737 * module, access should be Save, and the saved image will not contain an
740 internal bool IsModuleOnly {
742 return is_module_only;
745 is_module_only = value;
749 #if NET_2_0 || BOOTSTRAP_NET_2_0
750 ModuleBuilder manifest_module;
753 // MS.NET seems to return a ModuleBuilder when GetManifestModule () is called
754 // on an assemblybuilder.
756 internal override Module GetManifestModule () {
757 if (manifest_module == null)
758 manifest_module = DefineDynamicModule ("Default Dynamic Module");
759 return manifest_module;
768 void Save (string assemblyFileName, PortableExecutableKinds portableExecutableKind, ImageFileMachine imageFileMachine)
770 this.peKind = portableExecutableKind;
771 this.machine = imageFileMachine;
773 if (resource_writers != null) {
774 foreach (IResourceWriter writer in resource_writers) {
780 // Create a main module if not already created
781 ModuleBuilder mainModule = null;
782 if (modules != null) {
783 foreach (ModuleBuilder module in modules)
784 if (module.FullyQualifiedName == assemblyFileName)
787 if (mainModule == null)
788 mainModule = DefineDynamicModule ("RefEmit_OnDiskManifestModule", assemblyFileName);
791 mainModule.IsMain = true;
794 * Create a new entry point if the one specified
795 * by the user is in another module.
797 if ((entry_point != null) && entry_point.DeclaringType.Module != mainModule) {
799 if (entry_point.GetParameters ().Length == 1)
800 paramTypes = new Type [] { typeof (string) };
802 paramTypes = Type.EmptyTypes;
804 MethodBuilder mb = mainModule.DefineGlobalMethod ("__EntryPoint$", MethodAttributes.Static|MethodAttributes.PrivateScope, entry_point.ReturnType, paramTypes);
805 ILGenerator ilgen = mb.GetILGenerator ();
806 if (paramTypes.Length == 1)
807 ilgen.Emit (OpCodes.Ldarg_0);
808 ilgen.Emit (OpCodes.Tailcall);
809 ilgen.Emit (OpCodes.Call, entry_point);
810 ilgen.Emit (OpCodes.Ret);
815 if (version_res != null)
816 DefineVersionInfoResourceImpl (assemblyFileName);
819 // runtime needs to value to embed it into the assembly
820 public_key = sn.PublicKey;
823 foreach (ModuleBuilder module in modules)
824 if (module != mainModule)
827 // Write out the main module at the end, because it needs to
828 // contain the hash of the other modules
831 if ((sn != null) && (sn.CanSign)) {
832 sn.Sign (System.IO.Path.Combine (this.AssemblyDir, assemblyFileName));
838 public void Save (string assemblyFileName)
840 Save (assemblyFileName, PortableExecutableKinds.ILOnly, ImageFileMachine.I386);
843 public void SetEntryPoint (MethodInfo entryMethod)
845 SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
848 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
850 if (entryMethod == null)
851 throw new ArgumentNullException ("entryMethod");
852 if (entryMethod.DeclaringType.Assembly != this)
853 throw new InvalidOperationException ("Entry method is not defined in the same assembly.");
855 entry_point = entryMethod;
859 public void SetCustomAttribute( CustomAttributeBuilder customBuilder)
861 if (customBuilder == null)
862 throw new ArgumentNullException ("customBuilder");
864 if (IsCompilerContext) {
865 string attrname = customBuilder.Ctor.ReflectedType.FullName;
869 if (attrname == "System.Reflection.AssemblyVersionAttribute") {
870 version = create_assembly_version (customBuilder.string_arg ());
872 } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
873 culture = GetCultureString (customBuilder.string_arg ());
874 } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
875 data = customBuilder.Data;
877 algid = (uint) data [pos];
878 algid |= ((uint) data [pos + 1]) << 8;
879 algid |= ((uint) data [pos + 2]) << 16;
880 algid |= ((uint) data [pos + 3]) << 24;
881 } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
882 data = customBuilder.Data;
884 flags = (uint) data [pos];
885 flags |= ((uint) data [pos + 1]) << 8;
886 flags |= ((uint) data [pos + 2]) << 16;
887 flags |= ((uint) data [pos + 3]) << 24;
891 if (cattrs != null) {
892 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
893 cattrs.CopyTo (new_array, 0);
894 new_array [cattrs.Length] = customBuilder;
897 cattrs = new CustomAttributeBuilder [1];
898 cattrs [0] = customBuilder;
905 public void SetCustomAttribute ( ConstructorInfo con, byte[] binaryAttribute) {
907 throw new ArgumentNullException ("con");
908 if (binaryAttribute == null)
909 throw new ArgumentNullException ("binaryAttribute");
911 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
914 internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
915 this.corlib_object_type = corlib_object_type;
916 this.corlib_value_type = corlib_value_type;
917 this.corlib_enum_type = corlib_enum_type;
920 internal void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type)
922 SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type);
923 this.corlib_void_type = corlib_void_type;
926 private Exception not_supported () {
927 // Strange message but this is what MS.NET prints...
928 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
931 private void check_name_and_filename (string name, string fileName,
932 bool fileNeedsToExists) {
934 throw new ArgumentNullException ("name");
935 if (fileName == null)
936 throw new ArgumentNullException ("fileName");
937 if (name.Length == 0)
938 throw new ArgumentException ("Empty name is not legal.", "name");
939 if (fileName.Length == 0)
940 throw new ArgumentException ("Empty file name is not legal.", "fileName");
941 if (Path.GetFileName (fileName) != fileName)
942 throw new ArgumentException ("fileName '" + fileName + "' must not include a path.", "fileName");
944 // Resource files are created/searched under the assembly storage
946 string fullFileName = fileName;
948 fullFileName = Path.Combine (dir, fileName);
950 if (fileNeedsToExists && !File.Exists (fullFileName))
951 throw new FileNotFoundException ("Could not find file '" + fileName + "'");
953 if (resources != null) {
954 for (int i = 0; i < resources.Length; ++i) {
955 if (resources [i].filename == fullFileName)
956 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
957 if (resources [i].name == name)
958 throw new ArgumentException ("Duplicate name '" + name + "'");
962 if (modules != null) {
963 for (int i = 0; i < modules.Length; ++i) {
964 // Use fileName instead of fullFileName here
965 if (!modules [i].IsTransient () && (modules [i].FileName == fileName))
966 throw new ArgumentException ("Duplicate file name '" + fileName + "'");
967 if (modules [i].Name == name)
968 throw new ArgumentException ("Duplicate name '" + name + "'");
973 private String create_assembly_version (String version) {
974 String[] parts = version.Split ('.');
975 int[] ver = new int [4] { 0, 0, 0, 0 };
977 if ((parts.Length < 0) || (parts.Length > 4))
978 throw new ArgumentException ("The version specified '" + version + "' is invalid");
980 for (int i = 0; i < parts.Length; ++i) {
981 if (parts [i] == "*") {
982 DateTime now = DateTime.Now;
985 ver [2] = (now - new DateTime (2000, 1, 1)).Days;
986 if (parts.Length == 3)
987 ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
991 ver [3] = (now.Second + (now.Minute * 60) + (now.Hour * 3600)) / 2;
993 throw new ArgumentException ("The version specified '" + version + "' is invalid");
997 ver [i] = Int32.Parse (parts [i]);
999 catch (FormatException) {
1000 throw new ArgumentException ("The version specified '" + version + "' is invalid");
1005 return ver [0] + "." + ver [1] + "." + ver [2] + "." + ver [3];
1008 private string GetCultureString (string str)
1010 return (str == "neutral" ? String.Empty : str);
1013 internal override AssemblyName UnprotectedGetName ()
1015 AssemblyName an = base.UnprotectedGetName ();
1017 an.SetPublicKey (sn.PublicKey);
1018 an.SetPublicKeyToken (sn.PublicKeyToken);
1023 void _AssemblyBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
1025 throw new NotImplementedException ();
1028 void _AssemblyBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
1030 throw new NotImplementedException ();
1033 void _AssemblyBuilder.GetTypeInfoCount (out uint pcTInfo)
1035 throw new NotImplementedException ();
1038 void _AssemblyBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
1040 throw new NotImplementedException ();