Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AssemblyBuilder.cs « System.Reflection.Emit « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6c5cb539acfcbe054ea0266216af697ea09d099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// 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;

namespace System.Reflection.Emit {

	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 int[] table_indexes;
		internal ArrayList methods;

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern void basic_init (AssemblyBuilder ab);
		
		internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
			name = n.Name;
			dir = directory;
			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 .<Module> in TypeDef table */
				table_indexes [0x02] = 2;
			}
			// Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
			if (inc) {
				if ((table == 0x06) && (methods != null))
					methods.Add (obj);
				return table_indexes [table]++;
			}
			return table_indexes [table];
		}

		public override string CodeBase {
			get {
				return null;
			}
		}
		
		public override MethodInfo EntryPoint {
			get {
				return entry_point;
			}
		}

		public override string Location {
			get {
				return null;
			}
		}

		public void AddResourceFile (string name, string fileName)
		{
		}

		public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
		{
		}

		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);

			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 null;
		}

		public IResourceWriter DefineResource (string name, string description,
						       string fileName, ResourceAttributes attribute)
		{
			return null;
		}

		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)
		{
			return null;
		}

		public override Type[] GetExportedTypes ()
		{
			return null;
		}

		public override FileStream GetFile (string name)
		{
			return null;
		}

		/*public virtual FileStream[] GetFiles() {
			return null;
		}
		public override FileStream[] GetFiles(bool getResourceModules) {
			return null;
		}*/

		/*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
		  {
			return null;
		}
		public virtual string[] GetManifestResourceNames() {
			return null;
		}
		public virtual Stream GetManifestResourceStream(string name) {
			return null;
		}
		public virtual Stream GetManifestResourceStream(Type type, string name) {
			return null;
		}*/

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern int getUSIndex (AssemblyBuilder ab, string str);

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private static extern int getToken (AssemblyBuilder ab, MemberInfo member);

		internal int GetToken (string str) {
			return getUSIndex (this, str);
		}
		
		internal int GetToken (MemberInfo member) {
			return getToken (this, member);
		}
		
		[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 (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 ();
		}

		public void SetEntryPoint (MethodInfo entryMethod)
		{
			entry_point = entryMethod;
		}

		public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
		{
			entry_point = entryMethod;
		}

		public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
			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) {
			SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
		}

	}
}