AssemblyBuilder.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // System.Reflection.Emit/AssemblyBuilder.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.Reflection;
  11. using System.Resources;
  12. using System.IO;
  13. using System.Security.Policy;
  14. using System.Runtime.Serialization;
  15. using System.Globalization;
  16. using System.Runtime.CompilerServices;
  17. using System.Collections;
  18. namespace System.Reflection.Emit {
  19. internal struct MonoResource {
  20. public byte[] data;
  21. public string name;
  22. public string filename;
  23. public ResourceAttributes attrs;
  24. }
  25. public sealed class AssemblyBuilder : Assembly {
  26. private IntPtr dynamic_assembly;
  27. private MethodInfo entry_point;
  28. private ModuleBuilder[] modules;
  29. private string name;
  30. private string dir;
  31. private CustomAttributeBuilder[] cattrs;
  32. private MonoResource[] resources;
  33. internal Type corlib_object_type = typeof (System.Object);
  34. internal Type corlib_value_type = typeof (System.ValueType);
  35. internal Type corlib_enum_type = typeof (System.Enum);
  36. private int[] table_indexes;
  37. internal ArrayList methods;
  38. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  39. private static extern void basic_init (AssemblyBuilder ab);
  40. internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
  41. name = n.Name;
  42. dir = directory;
  43. basic_init (this);
  44. }
  45. internal int get_next_table_index (object obj, int table, bool inc) {
  46. if (table_indexes == null) {
  47. table_indexes = new int [64];
  48. for (int i=0; i < 64; ++i)
  49. table_indexes [i] = 1;
  50. /* allow room for .<Module> in TypeDef table */
  51. table_indexes [0x02] = 2;
  52. }
  53. // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
  54. if (inc) {
  55. if ((table == 0x06) && (methods != null))
  56. methods.Add (obj);
  57. return table_indexes [table]++;
  58. }
  59. return table_indexes [table];
  60. }
  61. public override string CodeBase {
  62. get {
  63. return null;
  64. }
  65. }
  66. public override MethodInfo EntryPoint {
  67. get {
  68. return entry_point;
  69. }
  70. }
  71. public override string Location {
  72. get {
  73. return null;
  74. }
  75. }
  76. public void AddResourceFile (string name, string fileName)
  77. {
  78. AddResourceFile (name, fileName, ResourceAttributes.Public);
  79. }
  80. public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
  81. {
  82. if (resources != null) {
  83. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  84. System.Array.Copy(resources, new_r, resources.Length);
  85. resources = new_r;
  86. } else {
  87. resources = new MonoResource [1];
  88. }
  89. int p = resources.Length - 1;
  90. resources [p].name = name;
  91. resources [p].filename = fileName;
  92. resources [p].attrs = attribute;
  93. }
  94. public void EmbedResourceFile (string name, string fileName)
  95. {
  96. EmbedResourceFile (name, fileName, ResourceAttributes.Public);
  97. }
  98. public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
  99. {
  100. if (resources != null) {
  101. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  102. System.Array.Copy(resources, new_r, resources.Length);
  103. resources = new_r;
  104. } else {
  105. resources = new MonoResource [1];
  106. }
  107. int p = resources.Length - 1;
  108. resources [p].name = name;
  109. resources [p].attrs = attribute;
  110. try {
  111. FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  112. long len = s.Length;
  113. resources [p].data = new byte [len];
  114. s.Read (resources [p].data, 0, (int)len);
  115. s.Close ();
  116. } catch {
  117. /* do something */
  118. }
  119. }
  120. public ModuleBuilder DefineDynamicModule (string name)
  121. {
  122. return DefineDynamicModule (name, name, false);
  123. }
  124. public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
  125. {
  126. return DefineDynamicModule (name, name, emitSymbolInfo);
  127. }
  128. public ModuleBuilder DefineDynamicModule(string name, string fileName)
  129. {
  130. return DefineDynamicModule (name, fileName, false);
  131. }
  132. public ModuleBuilder DefineDynamicModule (string name, string fileName,
  133. bool emitSymbolInfo)
  134. {
  135. ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo);
  136. if (modules != null) {
  137. ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
  138. System.Array.Copy(modules, new_modules, modules.Length);
  139. new_modules [modules.Length] = r;
  140. modules = new_modules;
  141. } else {
  142. modules = new ModuleBuilder [1];
  143. modules [0] = r;
  144. }
  145. return r;
  146. }
  147. public IResourceWriter DefineResource (string name, string description, string fileName)
  148. {
  149. return DefineResource (name, description, fileName, ResourceAttributes.Public);
  150. }
  151. public IResourceWriter DefineResource (string name, string description,
  152. string fileName, ResourceAttributes attribute)
  153. {
  154. return null;
  155. }
  156. public void DefineUnmanagedResource (byte[] resource)
  157. {
  158. }
  159. public void DefineUnmanagedResource (string resourceFileName)
  160. {
  161. }
  162. public void DefineVersionInfoResource ()
  163. {
  164. }
  165. public void DefineVersionInfoResource (string product, string productVersion,
  166. string company, string copyright, string trademark)
  167. {
  168. }
  169. public ModuleBuilder GetDynamicModule (string name)
  170. {
  171. return null;
  172. }
  173. public override Type[] GetExportedTypes ()
  174. {
  175. return null;
  176. }
  177. public override FileStream GetFile (string name)
  178. {
  179. return null;
  180. }
  181. /*public virtual FileStream[] GetFiles() {
  182. return null;
  183. }
  184. public override FileStream[] GetFiles(bool getResourceModules) {
  185. return null;
  186. }*/
  187. /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
  188. {
  189. return null;
  190. }
  191. public virtual string[] GetManifestResourceNames() {
  192. return null;
  193. }
  194. public virtual Stream GetManifestResourceStream(string name) {
  195. return null;
  196. }
  197. public virtual Stream GetManifestResourceStream(Type type, string name) {
  198. return null;
  199. }*/
  200. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  201. private static extern int getUSIndex (AssemblyBuilder ab, string str);
  202. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  203. private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
  204. internal int GetToken (string str) {
  205. return getUSIndex (this, str);
  206. }
  207. internal int GetToken (MemberInfo member) {
  208. return getToken (this, member);
  209. }
  210. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  211. private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
  212. public void Save (string assemblyFileName)
  213. {
  214. byte[] buf = new byte [65536];
  215. FileStream file;
  216. int count, offset;
  217. if (dir != null) {
  218. assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
  219. }
  220. file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
  221. offset = 0;
  222. while ((count = getDataChunk (this, buf, offset)) != 0) {
  223. file.Write (buf, 0, count);
  224. offset += count;
  225. }
  226. file.Close ();
  227. }
  228. public void SetEntryPoint (MethodInfo entryMethod)
  229. {
  230. entry_point = entryMethod;
  231. }
  232. public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
  233. {
  234. entry_point = entryMethod;
  235. }
  236. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  237. if (cattrs != null) {
  238. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  239. cattrs.CopyTo (new_array, 0);
  240. new_array [cattrs.Length] = customBuilder;
  241. cattrs = new_array;
  242. } else {
  243. cattrs = new CustomAttributeBuilder [1];
  244. cattrs [0] = customBuilder;
  245. }
  246. }
  247. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  248. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  249. }
  250. public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
  251. this.corlib_object_type = corlib_object_type;
  252. this.corlib_value_type = corlib_value_type;
  253. this.corlib_enum_type = corlib_enum_type;
  254. }
  255. }
  256. }