Mon Nov 19 13:58:01 CET 2001 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / corlib / System.Reflection.Emit / AssemblyBuilder.cs
1 //
2 // System.Reflection.Emit/AssemblyBuilder.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
7 // (C) 2001 Ximian, Inc.  http://www.ximian.com
8 //
9
10 using System;
11 using System.Reflection;
12 using System.Resources;
13 using System.IO;
14 using System.Security.Policy;
15 using System.Runtime.Serialization;
16 using System.Globalization;
17 using System.Runtime.CompilerServices;
18
19 namespace System.Reflection.Emit {
20
21         public sealed class AssemblyBuilder : Assembly {
22                 private IntPtr dynamic_assembly;
23                 private MethodInfo entry_point;
24                 private ModuleBuilder[] modules;
25                 private string name;
26                 private int[] table_indexes;
27
28                 internal AssemblyBuilder (AssemblyName n, AssemblyBuilderAccess access) {
29                         name = n.Name;
30                 }
31
32                 internal int get_next_table_index (int table, bool inc) {
33                         if (table_indexes == null) {
34                                 table_indexes = new int [64];
35                                 for (int i=0; i < 64; ++i)
36                                         table_indexes [i] = 1;
37                         }
38                         if (inc)
39                                 return table_indexes [table]++;
40                         return table_indexes [table];
41                 }
42
43                 public override string CodeBase {
44                         get {
45                                 return null;
46                         }
47                 }
48                 
49                 public override MethodInfo EntryPoint {
50                         get {
51                                 return entry_point;
52                         }
53                 }
54
55                 public override string Location {
56                         get {
57                                 return null;
58                         }
59                 }
60
61                 public void AddResourceFile (string name, string fileName)
62                 {
63                 }
64
65                 public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
66                 {
67                 }
68
69                 public override ModuleBuilder DefineDynamicModule (string name)
70                 {
71                         return DefineDynamicModule (name, name, false);
72                 }
73
74                 public override ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
75                 {
76                         return DefineDynamicModule (name, name, emitSymbolInfo);
77                 }
78
79                 public ModuleBuilder DefineDynamicModule(string name, string fileName)
80                 {
81                         return DefineDynamicModule (name, fileName, false);
82                 }
83
84                 public ModuleBuilder DefineDynamicModule (string name, string fileName,
85                                                           bool emitSymbolInfo)
86                 {
87                         ModuleBuilder r = new ModuleBuilder (this, name, fileName);
88
89                         if (modules != null) {
90                                 ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
91                                 System.Array.Copy(modules, new_modules, modules.Length);
92                                 new_modules [modules.Length] = r;
93                                 modules = new_modules;
94                         } else {
95                                 modules = new ModuleBuilder [1];
96                                 modules [0] = r;
97                         }
98                         return r;
99                 }
100
101                 public IResourceWriter DefineResource (string name, string description, string fileName)
102                 {
103                         return null;
104                 }
105
106                 public IResourceWriter DefineResource (string name, string description,
107                                                        string fileName, ResourceAttributes attribute)
108                 {
109                         return null;
110                 }
111
112                 public void DefineUnmanagedResource (byte[] resource)
113                 {
114                 }
115
116                 public void DefineUnmanagedResource (string resourceFileName)
117                 {
118                 }
119
120                 public void DefineVersionInfoResource ()
121                 {
122                 }
123
124                 public void DefineVersionInfoResource (string product, string productVersion,
125                                                        string company, string copyright, string trademark)
126                 {
127                 }
128
129                 public ModuleBuilder GetDynamicModule (string name)
130                 {
131                         return null;
132                 }
133
134                 public override Type[] GetExportedTypes ()
135                 {
136                         return null;
137                 }
138
139                 public override FileStream GetFile (string name)
140                 {
141                         return null;
142                 }
143
144                 /*public virtual FileStream[] GetFiles() {
145                         return null;
146                 }
147                 public override FileStream[] GetFiles(bool getResourceModules) {
148                         return null;
149                 }*/
150
151                 /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
152                   {
153                         return null;
154                 }
155                 public virtual string[] GetManifestResourceNames() {
156                         return null;
157                 }
158                 public virtual Stream GetManifestResourceStream(string name) {
159                         return null;
160                 }
161                 public virtual Stream GetManifestResourceStream(Type type, string name) {
162                         return null;
163                 }*/
164
165                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
166                 private static extern int getUSIndex (AssemblyBuilder ab, string str);
167
168                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
169                 private static extern int getDataChunk (AssemblyBuilder ab, int type, byte[] buf);
170
171                 public void Save (string assemblyFileName)
172                 {
173                         byte[] buf = new byte[8192];
174                         FileStream file;
175                         int count;
176
177                         file = new FileStream (assemblyFileName, FileMode.OpenOrCreate, FileAccess.Write);
178
179                         count = getDataChunk (this, 0, buf);
180                         if (count != 0) {
181                                 file.Write (buf, 0, count);
182                                 count = getDataChunk (this, 1, buf); /* may be a too small buffer */
183                                 file.Write (buf, 0, count);
184                         }
185
186                         file.Close ();
187                 }
188
189                 public void SetEntryPoint (MethodInfo entryMethod)
190                 {
191                         entry_point = entryMethod;
192                 }
193
194                 public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
195                 {
196                         entry_point = entryMethod;
197                 }
198
199         }
200 }