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 internal struct MonoResource {
25 public string filename;
26 public ResourceAttributes attrs;
29 public sealed class AssemblyBuilder : Assembly {
30 private IntPtr dynamic_assembly;
31 private MethodInfo entry_point;
32 private ModuleBuilder[] modules;
35 private CustomAttributeBuilder[] cattrs;
36 private MonoResource[] resources;
37 internal Type corlib_object_type = typeof (System.Object);
38 internal Type corlib_value_type = typeof (System.ValueType);
39 internal Type corlib_enum_type = typeof (System.Enum);
40 private int[] table_indexes;
41 internal ArrayList methods;
43 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44 private static extern void basic_init (AssemblyBuilder ab);
46 internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
52 internal int get_next_table_index (object obj, int table, bool inc) {
53 if (table_indexes == null) {
54 table_indexes = new int [64];
55 for (int i=0; i < 64; ++i)
56 table_indexes [i] = 1;
57 /* allow room for .<Module> in TypeDef table */
58 table_indexes [0x02] = 2;
60 // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
62 if ((table == 0x06) && (methods != null))
64 return table_indexes [table]++;
66 return table_indexes [table];
69 public override string CodeBase {
75 public override MethodInfo EntryPoint {
81 public override string Location {
87 public void AddResourceFile (string name, string fileName)
89 AddResourceFile (name, fileName, ResourceAttributes.Public);
92 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
94 if (resources != null) {
95 MonoResource[] new_r = new MonoResource [resources.Length + 1];
96 System.Array.Copy(resources, new_r, resources.Length);
99 resources = new MonoResource [1];
101 int p = resources.Length - 1;
102 resources [p].name = name;
103 resources [p].filename = fileName;
104 resources [p].attrs = attribute;
107 public ModuleBuilder DefineDynamicModule (string name)
109 return DefineDynamicModule (name, name, false);
112 public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
114 return DefineDynamicModule (name, name, emitSymbolInfo);
117 public ModuleBuilder DefineDynamicModule(string name, string fileName)
119 return DefineDynamicModule (name, fileName, false);
122 public ModuleBuilder DefineDynamicModule (string name, string fileName,
125 ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo);
127 if (modules != null) {
128 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
129 System.Array.Copy(modules, new_modules, modules.Length);
130 new_modules [modules.Length] = r;
131 modules = new_modules;
133 modules = new ModuleBuilder [1];
139 public IResourceWriter DefineResource (string name, string description, string fileName)
141 return DefineResource (name, description, fileName, ResourceAttributes.Public);
144 public IResourceWriter DefineResource (string name, string description,
145 string fileName, ResourceAttributes attribute)
150 public void DefineUnmanagedResource (byte[] resource)
154 public void DefineUnmanagedResource (string resourceFileName)
158 public void DefineVersionInfoResource ()
162 public void DefineVersionInfoResource (string product, string productVersion,
163 string company, string copyright, string trademark)
167 public ModuleBuilder GetDynamicModule (string name)
172 public override Type[] GetExportedTypes ()
177 public override FileStream GetFile (string name)
182 /*public virtual FileStream[] GetFiles() {
185 public override FileStream[] GetFiles(bool getResourceModules) {
189 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
193 public virtual string[] GetManifestResourceNames() {
196 public virtual Stream GetManifestResourceStream(string name) {
199 public virtual Stream GetManifestResourceStream(Type type, string name) {
203 [MethodImplAttribute(MethodImplOptions.InternalCall)]
204 private static extern int getUSIndex (AssemblyBuilder ab, string str);
206 [MethodImplAttribute(MethodImplOptions.InternalCall)]
207 private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
209 internal int GetToken (string str) {
210 return getUSIndex (this, str);
213 internal int GetToken (MemberInfo member) {
214 return getToken (this, member);
217 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218 private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
220 public void Save (string assemblyFileName)
222 byte[] buf = new byte [65536];
227 assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
230 file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
233 while ((count = getDataChunk (this, buf, offset)) != 0) {
234 file.Write (buf, 0, count);
240 public void SetEntryPoint (MethodInfo entryMethod)
242 entry_point = entryMethod;
245 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
247 entry_point = entryMethod;
250 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
251 if (cattrs != null) {
252 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
253 cattrs.CopyTo (new_array, 0);
254 new_array [cattrs.Length] = customBuilder;
257 cattrs = new CustomAttributeBuilder [1];
258 cattrs [0] = customBuilder;
261 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
262 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
265 public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
266 this.corlib_object_type = corlib_object_type;
267 this.corlib_value_type = corlib_value_type;
268 this.corlib_enum_type = corlib_enum_type;