// // System.Reflection.Emit/AssemblyBuilder.cs // // Author: // Paolo Molaro (lupus@ximian.com) // // (C) 2001 Ximian, Inc. http://www.ximian.com // using System; using System.Reflection; using System.Resources; using System.IO; using System.Security.Policy; using System.Runtime.Serialization; using System.Globalization; using System.Runtime.CompilerServices; using System.Collections; using System.Runtime.InteropServices; namespace System.Reflection.Emit { internal struct MonoResource { public byte[] data; public string name; public string filename; public ResourceAttributes attrs; } public sealed class AssemblyBuilder : Assembly { private IntPtr dynamic_assembly; private MethodInfo entry_point; private ModuleBuilder[] modules; private string name; private string dir; private CustomAttributeBuilder[] cattrs; private MonoResource[] resources; string keyfile; string version; string culture; uint algid; uint flags; PEFileKinds pekind = PEFileKinds.Dll; bool delay_sign; uint access; internal Type corlib_object_type = typeof (System.Object); internal Type corlib_value_type = typeof (System.ValueType); internal Type corlib_enum_type = typeof (System.Enum); internal Type corlib_void_type = typeof (void); private int[] table_indexes; Hashtable us_string_cache = new Hashtable (); ArrayList resource_writers = null; [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void basic_init (AssemblyBuilder ab); internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) { name = n.Name; dir = directory; this.access = (uint)access; basic_init (this); } internal int get_next_table_index (object obj, int table, bool inc) { if (table_indexes == null) { table_indexes = new int [64]; for (int i=0; i < 64; ++i) table_indexes [i] = 1; /* allow room for . in TypeDef table */ table_indexes [0x02] = 2; } // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString()); if (inc) return table_indexes [table]++; return table_indexes [table]; } public override string CodeBase { get { throw not_supported (); } } public override MethodInfo EntryPoint { get { return entry_point; } } public override string Location { get { return null; } } public void AddResourceFile (string name, string fileName) { AddResourceFile (name, fileName, ResourceAttributes.Public); } public void AddResourceFile (string name, string fileName, ResourceAttributes attribute) { if (resources != null) { MonoResource[] new_r = new MonoResource [resources.Length + 1]; System.Array.Copy(resources, new_r, resources.Length); resources = new_r; } else { resources = new MonoResource [1]; } int p = resources.Length - 1; resources [p].name = name; resources [p].filename = fileName; resources [p].attrs = attribute; } public void EmbedResourceFile (string name, string fileName) { EmbedResourceFile (name, fileName, ResourceAttributes.Public); } public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute) { if (resources != null) { MonoResource[] new_r = new MonoResource [resources.Length + 1]; System.Array.Copy(resources, new_r, resources.Length); resources = new_r; } else { resources = new MonoResource [1]; } int p = resources.Length - 1; resources [p].name = name; resources [p].attrs = attribute; try { FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read); long len = s.Length; resources [p].data = new byte [len]; s.Read (resources [p].data, 0, (int)len); s.Close (); } catch { /* do something */ } } internal void EmbedResource (string name, byte[] blob, ResourceAttributes attribute) { if (resources != null) { MonoResource[] new_r = new MonoResource [resources.Length + 1]; System.Array.Copy(resources, new_r, resources.Length); resources = new_r; } else { resources = new MonoResource [1]; } int p = resources.Length - 1; resources [p].name = name; resources [p].attrs = attribute; resources [p].data = blob; } public ModuleBuilder DefineDynamicModule (string name) { return DefineDynamicModule (name, name, false); } public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo) { return DefineDynamicModule (name, name, emitSymbolInfo); } public ModuleBuilder DefineDynamicModule(string name, string fileName) { return DefineDynamicModule (name, fileName, false); } public ModuleBuilder DefineDynamicModule (string name, string fileName, bool emitSymbolInfo) { ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo, modules == null); if (modules != null) { ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1]; System.Array.Copy(modules, new_modules, modules.Length); new_modules [modules.Length] = r; modules = new_modules; } else { modules = new ModuleBuilder [1]; modules [0] = r; } return r; } public IResourceWriter DefineResource (string name, string description, string fileName) { return DefineResource (name, description, fileName, ResourceAttributes.Public); } public IResourceWriter DefineResource (string name, string description, string fileName, ResourceAttributes attribute) { IResourceWriter writer; // description seems to be ignored AddResourceFile (name, fileName, attribute); writer = new ResourceWriter (fileName); if (resource_writers == null) resource_writers = new ArrayList (); resource_writers.Add (writer); return writer; } public void DefineUnmanagedResource (byte[] resource) { } public void DefineUnmanagedResource (string resourceFileName) { } public void DefineVersionInfoResource () { } public void DefineVersionInfoResource (string product, string productVersion, string company, string copyright, string trademark) { } public ModuleBuilder GetDynamicModule (string name) { if (name == null) throw new ArgumentNullException ("name"); if (name == "") throw new ArgumentException ("Name can't be null"); if (modules != null) for (int i = 0; i < modules.Length; ++i) if (modules [i].name == name) return modules [i]; return null; } public override Type[] GetExportedTypes () { throw not_supported (); } public override FileStream GetFile (string name) { throw not_supported (); } public override FileStream[] GetFiles() { throw not_supported (); } public override FileStream[] GetFiles(bool getResourceModules) { throw not_supported (); } public override ManifestResourceInfo GetManifestResourceInfo(string resourceName) { throw not_supported (); } public override string[] GetManifestResourceNames() { throw not_supported (); } public override Stream GetManifestResourceStream(string name) { throw not_supported (); } public override Stream GetManifestResourceStream(Type type, string name) { throw not_supported (); } [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int getUSIndex (AssemblyBuilder ab, string str); [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int getToken (AssemblyBuilder ab, object obj); internal int GetToken (string str) { if (us_string_cache.Contains (str)) return (int)us_string_cache [str]; int result = getUSIndex (this, str); us_string_cache [str] = result; return result; } internal int GetToken (MemberInfo member) { return getToken (this, member); } internal int GetToken (SignatureHelper helper) { return getToken (this, helper); } internal bool IsSave { get { return access != (uint)AssemblyBuilderAccess.Run; } } [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset); public void Save (string assemblyFileName) { byte[] buf = new byte [65536]; FileStream file; int count, offset; if (resource_writers != null) { foreach (IResourceWriter writer in resource_writers) { writer.Generate (); writer.Close (); } } build_metadata (this); foreach (ModuleBuilder module in modules) module.Save (); if (dir != null) { assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName); } file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write); offset = 0; while ((count = getDataChunk (this, buf, offset)) != 0) { file.Write (buf, 0, count); offset += count; } file.Close (); // // The constant 0x80000000 is internal to Mono, it means `make executable' // File.SetAttributes (assemblyFileName, (FileAttributes) (unchecked ((int) 0x80000000))); } [MethodImplAttribute(MethodImplOptions.InternalCall)] private static extern void build_metadata (AssemblyBuilder ab); public void SetEntryPoint (MethodInfo entryMethod) { SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication); } public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind) { if (entryMethod == null) throw new ArgumentNullException ("entryMethod"); if (entryMethod.DeclaringType.Assembly != this) throw new InvalidOperationException ("Entry method is not defined in the same assembly."); entry_point = entryMethod; pekind = fileKind; } public void SetCustomAttribute( CustomAttributeBuilder customBuilder) { if (customBuilder == null) throw new ArgumentNullException ("customBuilder"); string attrname = customBuilder.Ctor.ReflectedType.FullName; byte[] data; int len, pos; if (attrname == "System.Reflection.AssemblyVersionAttribute") { data = customBuilder.Data; pos = 2; len = CustomAttributeBuilder.decode_len (data, pos, out pos); version = CustomAttributeBuilder.string_from_bytes (data, pos, len); return; } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") { data = customBuilder.Data; pos = 2; len = CustomAttributeBuilder.decode_len (data, pos, out pos); keyfile = CustomAttributeBuilder.string_from_bytes (data, pos, len); } else if (attrname == "System.Reflection.AssemblyCultureAttribute") { data = customBuilder.Data; pos = 2; len = CustomAttributeBuilder.decode_len (data, pos, out pos); culture = CustomAttributeBuilder.string_from_bytes (data, pos, len); } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") { data = customBuilder.Data; pos = 2; algid = (uint)data [pos]; algid |= ((uint)data [pos + 1]) << 8; algid |= ((uint)data [pos + 2]) << 16; algid |= ((uint)data [pos + 3]) << 24; } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") { data = customBuilder.Data; pos = 2; flags = (uint)data [pos]; flags |= ((uint)data [pos + 1]) << 8; flags |= ((uint)data [pos + 2]) << 16; flags |= ((uint)data [pos + 3]) << 24; return; } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") { data = customBuilder.Data; pos = 2; delay_sign = data [2] != 0; } if (cattrs != null) { CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1]; cattrs.CopyTo (new_array, 0); new_array [cattrs.Length] = customBuilder; cattrs = new_array; } else { cattrs = new CustomAttributeBuilder [1]; cattrs [0] = customBuilder; } } public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) { if (con == null) throw new ArgumentNullException ("con"); if (binaryAttribute == null) throw new ArgumentNullException ("binaryAttribute"); SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute)); } public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) { this.corlib_object_type = corlib_object_type; this.corlib_value_type = corlib_value_type; this.corlib_enum_type = corlib_enum_type; } public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type, Type corlib_void_type) { SetCorlibTypeBuilders (corlib_object_type, corlib_value_type, corlib_enum_type); this.corlib_void_type = corlib_void_type; } private Exception not_supported () { // Strange message but this is what MS.NET prints... return new NotSupportedException ("The invoked member is not supported in a dynamic module."); } } }