2 // System.Reflection.Emit/AssemblyBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 using System.Reflection;
12 using System.Resources;
14 using System.Security.Policy;
15 using System.Runtime.Serialization;
16 using System.Globalization;
17 using System.Runtime.CompilerServices;
18 using System.Collections;
20 namespace System.Reflection.Emit {
22 public sealed class AssemblyBuilder : Assembly {
23 private IntPtr dynamic_assembly;
24 private MethodInfo entry_point;
25 private ModuleBuilder[] modules;
28 private CustomAttributeBuilder[] cattrs;
29 internal Type corlib_object_type = typeof (System.Object);
30 internal Type corlib_value_type = typeof (System.ValueType);
31 private int[] table_indexes;
32 internal ArrayList methods;
34 [MethodImplAttribute(MethodImplOptions.InternalCall)]
35 private static extern void basic_init (AssemblyBuilder ab);
37 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
43 internal int get_next_table_index (object obj, int table, bool inc) {
44 if (table_indexes == null) {
45 table_indexes = new int [64];
46 for (int i=0; i < 64; ++i)
47 table_indexes [i] = 1;
48 /* allow room for .<Module> in TypeDef table */
49 table_indexes [0x02] = 2;
51 // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
53 if ((table == 0x06) && (methods != null))
55 return table_indexes [table]++;
57 return table_indexes [table];
60 public override string CodeBase {
66 public override MethodInfo EntryPoint {
72 public override string Location {
78 public void AddResourceFile (string name, string fileName)
82 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
86 public ModuleBuilder DefineDynamicModule (string name)
88 return DefineDynamicModule (name, name, false);
91 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
93 return DefineDynamicModule (name, name, emitSymbolInfo);
96 public ModuleBuilder DefineDynamicModule(string name, string fileName)
98 return DefineDynamicModule (name, fileName, false);
101 public ModuleBuilder DefineDynamicModule (string name, string fileName,
104 ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo);
106 if (modules != null) {
107 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
108 System.Array.Copy(modules, new_modules, modules.Length);
109 new_modules [modules.Length] = r;
110 modules = new_modules;
112 modules = new ModuleBuilder [1];
118 public IResourceWriter DefineResource (string name, string description, string fileName)
123 public IResourceWriter DefineResource (string name, string description,
124 string fileName, ResourceAttributes attribute)
129 public void DefineUnmanagedResource (byte[] resource)
133 public void DefineUnmanagedResource (string resourceFileName)
137 public void DefineVersionInfoResource ()
141 public void DefineVersionInfoResource (string product, string productVersion,
142 string company, string copyright, string trademark)
146 public ModuleBuilder GetDynamicModule (string name)
151 public override Type[] GetExportedTypes ()
156 public override FileStream GetFile (string name)
161 /*public virtual FileStream[] GetFiles() {
164 public override FileStream[] GetFiles(bool getResourceModules) {
168 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
172 public virtual string[] GetManifestResourceNames() {
175 public virtual Stream GetManifestResourceStream(string name) {
178 public virtual Stream GetManifestResourceStream(Type type, string name) {
182 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183 private static extern int getUSIndex (AssemblyBuilder ab, string str);
185 [MethodImplAttribute(MethodImplOptions.InternalCall)]
186 private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
188 internal int GetToken (string str) {
189 return getUSIndex (this, str);
192 internal int GetToken (MemberInfo member) {
193 return getToken (this, member);
196 [MethodImplAttribute(MethodImplOptions.InternalCall)]
197 private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
199 public void Save (string assemblyFileName)
201 byte[] buf = new byte [65536];
206 assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
209 file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
212 while ((count = getDataChunk (this, buf, offset)) != 0) {
213 file.Write (buf, 0, count);
219 public void SetEntryPoint (MethodInfo entryMethod)
221 entry_point = entryMethod;
224 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
226 entry_point = entryMethod;
229 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
230 if (cattrs != null) {
231 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
232 cattrs.CopyTo (new_array, 0);
233 new_array [cattrs.Length] = customBuilder;
236 cattrs = new CustomAttributeBuilder [1];
237 cattrs [0] = customBuilder;
240 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
241 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
244 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type) {
245 this.corlib_object_type = corlib_object_type;
246 this.corlib_value_type = corlib_value_type;