AssemblyBuilder.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. string keyfile;
  34. string version;
  35. string culture;
  36. uint algid;
  37. uint flags;
  38. PEFileKinds pekind = PEFileKinds.Dll;
  39. bool delay_sign;
  40. internal Type corlib_object_type = typeof (System.Object);
  41. internal Type corlib_value_type = typeof (System.ValueType);
  42. internal Type corlib_enum_type = typeof (System.Enum);
  43. private int[] table_indexes;
  44. internal ArrayList methods;
  45. Hashtable us_string_cache = new Hashtable ();
  46. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  47. private static extern void basic_init (AssemblyBuilder ab);
  48. internal AssemblyBuilder (AssemblyName n, string directory, AssemblyBuilderAccess access) {
  49. name = n.Name;
  50. dir = directory;
  51. basic_init (this);
  52. }
  53. internal int get_next_table_index (object obj, int table, bool inc) {
  54. if (table_indexes == null) {
  55. table_indexes = new int [64];
  56. for (int i=0; i < 64; ++i)
  57. table_indexes [i] = 1;
  58. /* allow room for .<Module> in TypeDef table */
  59. table_indexes [0x02] = 2;
  60. }
  61. // Console.WriteLine ("getindex for table "+table.ToString()+" got "+table_indexes [table].ToString());
  62. if (inc) {
  63. if ((table == 0x06) && (methods != null))
  64. methods.Add (obj);
  65. return table_indexes [table]++;
  66. }
  67. return table_indexes [table];
  68. }
  69. public override string CodeBase {
  70. get {
  71. return null;
  72. }
  73. }
  74. public override MethodInfo EntryPoint {
  75. get {
  76. return entry_point;
  77. }
  78. }
  79. public override string Location {
  80. get {
  81. return null;
  82. }
  83. }
  84. public void AddResourceFile (string name, string fileName)
  85. {
  86. AddResourceFile (name, fileName, ResourceAttributes.Public);
  87. }
  88. public void AddResourceFile (string name, string fileName, ResourceAttributes attribute)
  89. {
  90. if (resources != null) {
  91. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  92. System.Array.Copy(resources, new_r, resources.Length);
  93. resources = new_r;
  94. } else {
  95. resources = new MonoResource [1];
  96. }
  97. int p = resources.Length - 1;
  98. resources [p].name = name;
  99. resources [p].filename = fileName;
  100. resources [p].attrs = attribute;
  101. }
  102. public void EmbedResourceFile (string name, string fileName)
  103. {
  104. EmbedResourceFile (name, fileName, ResourceAttributes.Public);
  105. }
  106. public void EmbedResourceFile (string name, string fileName, ResourceAttributes attribute)
  107. {
  108. if (resources != null) {
  109. MonoResource[] new_r = new MonoResource [resources.Length + 1];
  110. System.Array.Copy(resources, new_r, resources.Length);
  111. resources = new_r;
  112. } else {
  113. resources = new MonoResource [1];
  114. }
  115. int p = resources.Length - 1;
  116. resources [p].name = name;
  117. resources [p].attrs = attribute;
  118. try {
  119. FileStream s = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  120. long len = s.Length;
  121. resources [p].data = new byte [len];
  122. s.Read (resources [p].data, 0, (int)len);
  123. s.Close ();
  124. } catch {
  125. /* do something */
  126. }
  127. }
  128. public ModuleBuilder DefineDynamicModule (string name)
  129. {
  130. return DefineDynamicModule (name, name, false);
  131. }
  132. public ModuleBuilder DefineDynamicModule (string name, bool emitSymbolInfo)
  133. {
  134. return DefineDynamicModule (name, name, emitSymbolInfo);
  135. }
  136. public ModuleBuilder DefineDynamicModule(string name, string fileName)
  137. {
  138. return DefineDynamicModule (name, fileName, false);
  139. }
  140. public ModuleBuilder DefineDynamicModule (string name, string fileName,
  141. bool emitSymbolInfo)
  142. {
  143. ModuleBuilder r = new ModuleBuilder (this, name, fileName, emitSymbolInfo);
  144. if (modules != null) {
  145. ModuleBuilder[] new_modules = new ModuleBuilder [modules.Length + 1];
  146. System.Array.Copy(modules, new_modules, modules.Length);
  147. new_modules [modules.Length] = r;
  148. modules = new_modules;
  149. } else {
  150. modules = new ModuleBuilder [1];
  151. modules [0] = r;
  152. }
  153. return r;
  154. }
  155. public IResourceWriter DefineResource (string name, string description, string fileName)
  156. {
  157. return DefineResource (name, description, fileName, ResourceAttributes.Public);
  158. }
  159. public IResourceWriter DefineResource (string name, string description,
  160. string fileName, ResourceAttributes attribute)
  161. {
  162. return null;
  163. }
  164. public void DefineUnmanagedResource (byte[] resource)
  165. {
  166. }
  167. public void DefineUnmanagedResource (string resourceFileName)
  168. {
  169. }
  170. public void DefineVersionInfoResource ()
  171. {
  172. }
  173. public void DefineVersionInfoResource (string product, string productVersion,
  174. string company, string copyright, string trademark)
  175. {
  176. }
  177. public ModuleBuilder GetDynamicModule (string name)
  178. {
  179. return null;
  180. }
  181. public override Type[] GetExportedTypes ()
  182. {
  183. return null;
  184. }
  185. public override FileStream GetFile (string name)
  186. {
  187. return null;
  188. }
  189. /*public virtual FileStream[] GetFiles() {
  190. return null;
  191. }
  192. public override FileStream[] GetFiles(bool getResourceModules) {
  193. return null;
  194. }*/
  195. /*public virtual ManifestResourceInfo GetManifestResourceInfo(string resourceName)
  196. {
  197. return null;
  198. }
  199. public virtual string[] GetManifestResourceNames() {
  200. return null;
  201. }
  202. public virtual Stream GetManifestResourceStream(string name) {
  203. return null;
  204. }
  205. public virtual Stream GetManifestResourceStream(Type type, string name) {
  206. return null;
  207. }*/
  208. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  209. private static extern int getUSIndex (AssemblyBuilder ab, string str);
  210. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  211. private static extern int getToken (AssemblyBuilder ab, MemberInfo member);
  212. internal int GetToken (string str) {
  213. if (us_string_cache.Contains (str))
  214. return (int)us_string_cache [str];
  215. int result = getUSIndex (this, str);
  216. us_string_cache [str] = result;
  217. return result;
  218. }
  219. internal int GetToken (MemberInfo member) {
  220. return getToken (this, member);
  221. }
  222. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  223. private static extern int getDataChunk (AssemblyBuilder ab, byte[] buf, int offset);
  224. public void Save (string assemblyFileName)
  225. {
  226. byte[] buf = new byte [65536];
  227. FileStream file;
  228. int count, offset;
  229. if (dir != null) {
  230. assemblyFileName = String.Format ("{0}{1}{2}", dir, System.IO.Path.DirectorySeparatorChar, assemblyFileName);
  231. }
  232. file = new FileStream (assemblyFileName, FileMode.Create, FileAccess.Write);
  233. offset = 0;
  234. while ((count = getDataChunk (this, buf, offset)) != 0) {
  235. file.Write (buf, 0, count);
  236. offset += count;
  237. }
  238. file.Close ();
  239. }
  240. public void SetEntryPoint (MethodInfo entryMethod)
  241. {
  242. SetEntryPoint (entryMethod, PEFileKinds.ConsoleApplication);
  243. }
  244. public void SetEntryPoint (MethodInfo entryMethod, PEFileKinds fileKind)
  245. {
  246. entry_point = entryMethod;
  247. pekind = fileKind;
  248. }
  249. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  250. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  251. byte[] data;
  252. int len, pos;
  253. if (attrname == "System.Reflection.AssemblyVersionAttribute") {
  254. data = customBuilder.Data;
  255. pos = 2;
  256. len = CustomAttributeBuilder.decode_len (data, pos, out pos);
  257. version = CustomAttributeBuilder.string_from_bytes (data, pos, len);
  258. return;
  259. } else if (attrname == "System.Reflection.AssemblyKeyFileAttribute") {
  260. data = customBuilder.Data;
  261. pos = 2;
  262. len = CustomAttributeBuilder.decode_len (data, pos, out pos);
  263. keyfile = CustomAttributeBuilder.string_from_bytes (data, pos, len);
  264. } else if (attrname == "System.Reflection.AssemblyCultureAttribute") {
  265. data = customBuilder.Data;
  266. pos = 2;
  267. len = CustomAttributeBuilder.decode_len (data, pos, out pos);
  268. culture = CustomAttributeBuilder.string_from_bytes (data, pos, len);
  269. } else if (attrname == "System.Reflection.AssemblyAlgorithmIdAttribute") {
  270. data = customBuilder.Data;
  271. pos = 2;
  272. algid = (uint)data [pos];
  273. algid |= ((uint)data [pos + 1]) << 8;
  274. algid |= ((uint)data [pos + 2]) << 16;
  275. algid |= ((uint)data [pos + 3]) << 24;
  276. } else if (attrname == "System.Reflection.AssemblyFlagsAttribute") {
  277. data = customBuilder.Data;
  278. pos = 2;
  279. flags = (uint)data [pos];
  280. flags |= ((uint)data [pos + 1]) << 8;
  281. flags |= ((uint)data [pos + 2]) << 16;
  282. flags |= ((uint)data [pos + 3]) << 24;
  283. return;
  284. } else if (attrname == "System.Reflection.AssemblyDelaySignAttribute") {
  285. data = customBuilder.Data;
  286. pos = 2;
  287. delay_sign = data [2] != 0;
  288. }
  289. if (cattrs != null) {
  290. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  291. cattrs.CopyTo (new_array, 0);
  292. new_array [cattrs.Length] = customBuilder;
  293. cattrs = new_array;
  294. } else {
  295. cattrs = new CustomAttributeBuilder [1];
  296. cattrs [0] = customBuilder;
  297. }
  298. }
  299. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  300. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  301. }
  302. public void SetCorlibTypeBuilders (Type corlib_object_type, Type corlib_value_type, Type corlib_enum_type) {
  303. this.corlib_object_type = corlib_object_type;
  304. this.corlib_value_type = corlib_value_type;
  305. this.corlib_enum_type = corlib_enum_type;
  306. }
  307. }
  308. }