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;
19 namespace System.Reflection.Emit {
21 public sealed class AssemblyBuilder : Assembly {
22 private IntPtr dynamic_assembly;
23 private MethodInfo entry_point;
24 private ModuleBuilder[] modules;
26 private CustomAttributeBuilder[] cattrs;
27 private int[] table_indexes;
29 [MethodImplAttribute(MethodImplOptions.InternalCall)]
30 private static extern void basic_init (AssemblyBuilder ab);
32 internal AssemblyBuilder (AssemblyName n, AssemblyBuilderAccess access) {
37 internal int get_next_table_index (int table, bool inc) {
38 if (table_indexes == null) {
39 table_indexes = new int [64];
40 for (int i=0; i < 64; ++i)
41 table_indexes [i] = 1;
42 /* allow room for .<Module> in TypeDef table */
43 table_indexes [0x02] = 2;
45 // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
47 return table_indexes [table]++;
48 return table_indexes [table];
51 public override string CodeBase {
57 public override MethodInfo EntryPoint {
63 public override string Location {
69 public void AddResourceFile (string name, string fileName)
73 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
77 public override ModuleBuilder DefineDynamicModule (string name)
79 return DefineDynamicModule (name, name, false);
82 public override ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
84 return DefineDynamicModule (name, name, emitSymbolInfo);
87 public ModuleBuilder DefineDynamicModule(string name, string fileName)
89 return DefineDynamicModule (name, fileName, false);
92 public ModuleBuilder DefineDynamicModule (string name, string fileName,
95 ModuleBuilder r = new ModuleBuilder (this, name, fileName);
97 if (modules != null) {
98 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
99 System.Array.Copy(modules, new_modules, modules.Length);
100 new_modules [modules.Length] = r;
101 modules = new_modules;
103 modules = new ModuleBuilder [1];
109 public IResourceWriter DefineResource (string name, string description, string fileName)
114 public IResourceWriter DefineResource (string name, string description,
115 string fileName, ResourceAttributes attribute)
120 public void DefineUnmanagedResource (byte[] resource)
124 public void DefineUnmanagedResource (string resourceFileName)
128 public void DefineVersionInfoResource ()
132 public void DefineVersionInfoResource (string product, string productVersion,
133 string company, string copyright, string trademark)
137 public ModuleBuilder GetDynamicModule (string name)
142 public override Type[] GetExportedTypes ()
147 public override FileStream GetFile (string name)
152 /*public virtual FileStream[] GetFiles() {
155 public override FileStream[] GetFiles(bool getResourceModules) {
159 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
163 public virtual string[] GetManifestResourceNames() {
166 public virtual Stream GetManifestResourceStream(string name) {
169 public virtual Stream GetManifestResourceStream(Type type, string name) {
173 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174 private static extern int getUSIndex (AssemblyBuilder ab, string str);
176 [MethodImplAttribute(MethodImplOptions.InternalCall)]
177 private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
179 internal int GetToken (string str) {
180 return getUSIndex (this, str);
183 internal int GetToken (MemberInfo member) {
184 return getToken (this, member);
187 [MethodImplAttribute(MethodImplOptions.InternalCall)]
188 private static extern int getDataChunk (AssemblyBuilder ab, int type, byte[] buf);
190 public void Save (string assemblyFileName)
192 byte[] buf = new byte [1024000];
196 file = new FileStream (assemblyFileName, FileMode.OpenOrCreate, FileAccess.Write);
198 count = getDataChunk (this, 0, buf);
200 file.Write (buf, 0, count);
202 while ((count = getDataChunk (this, offset, buf)) != 0) {
203 file.Write (buf, 0, count);
211 public void SetEntryPoint (MethodInfo entryMethod)
213 entry_point = entryMethod;
216 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
218 entry_point = entryMethod;
221 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
222 if (cattrs != null) {
223 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
224 cattrs.CopyTo (new_array, 0);
225 new_array [cattrs.Length] = customBuilder;
228 cattrs = new CustomAttributeBuilder [1];
229 cattrs [0] = customBuilder;
232 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
233 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));