ModuleBuilder.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // System.Reflection.Emit/ModuleBuilder.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.Runtime.CompilerServices;
  12. using System.Runtime.InteropServices;
  13. namespace System.Reflection.Emit {
  14. public class ModuleBuilder : Module {
  15. private TypeBuilder[] types;
  16. private CustomAttributeBuilder[] cattrs;
  17. private int table_idx;
  18. private AssemblyBuilder assemblyb;
  19. internal ModuleBuilder (AssemblyBuilder assb, string name, string fullyqname) {
  20. this.name = this.scopename = name;
  21. this.fqname = fullyqname;
  22. this.assembly = this.assemblyb = assb;
  23. table_idx = get_next_table_index (0x00, true);
  24. }
  25. public override string FullyQualifiedName {get { return fqname;}}
  26. [MonoTODO]
  27. public TypeBuilder DefineType (string name) {
  28. // FIXME: LAMESPEC: what other attributes should we use here as default?
  29. return DefineType (name, TypeAttributes.Public, typeof(object), null);
  30. }
  31. public TypeBuilder DefineType (string name, TypeAttributes attr) {
  32. return DefineType (name, attr, typeof(object), null);
  33. }
  34. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent) {
  35. return DefineType (name, attr, parent, null);
  36. }
  37. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, Type[] interfaces) {
  38. TypeBuilder res = new TypeBuilder (this, name, attr, parent, interfaces);
  39. if (types != null) {
  40. TypeBuilder[] new_types = new TypeBuilder [types.Length + 1];
  41. System.Array.Copy (types, new_types, types.Length);
  42. new_types [types.Length] = res;
  43. types = new_types;
  44. } else {
  45. types = new TypeBuilder [1];
  46. types [0] = res;
  47. }
  48. return res;
  49. }
  50. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, int typesize) {
  51. return DefineType (name, attr, parent, null);
  52. }
  53. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize) {
  54. return DefineType (name, attr, parent, null);
  55. }
  56. public TypeBuilder DefineType (string name, TypeAttributes attr, Type parent, PackingSize packsize, int typesize) {
  57. return DefineType (name, attr, parent, null);
  58. }
  59. public MethodInfo GetArrayMethod( Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  60. return null;
  61. }
  62. public EnumBuilder DefineEnum( string name, TypeAttributes visibility, Type underlyingType) {
  63. EnumBuilder eb = new EnumBuilder (this, name, visibility, underlyingType);
  64. return eb;
  65. }
  66. public override Type GetType( string className) {
  67. return GetType (className, false, false);
  68. }
  69. public override Type GetType( string className, bool ignoreCase) {
  70. return GetType (className, false, ignoreCase);
  71. }
  72. private TypeBuilder search_in_array (TypeBuilder[] arr, string className, bool ignoreCase) {
  73. int i;
  74. for (i = 0; i < arr.Length; ++i) {
  75. if (String.Compare (className, arr [i].FullName, ignoreCase) == 0) {
  76. return arr [i];
  77. }
  78. }
  79. return null;
  80. }
  81. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  82. private static extern Type create_modified_type (TypeBuilder tb, int arrayrank, bool isbyref);
  83. public override Type GetType( string className, bool throwOnError, bool ignoreCase) {
  84. int subt;
  85. bool isbyref;
  86. int arrayrank = 0;
  87. TypeBuilder result = null;
  88. if (types == null && throwOnError)
  89. throw new TypeLoadException (className);
  90. subt = className.IndexOf ('&');
  91. if (subt >= 0) {
  92. isbyref = true;
  93. className = className.Substring (0, subt);
  94. } else
  95. isbyref = false;
  96. subt = className.IndexOf ('[');
  97. if (subt >= 0) {
  98. arrayrank = 1;
  99. // FIXME: support different ranks..
  100. className = className.Substring (0, subt);
  101. }
  102. subt = className.IndexOf ('+');
  103. if (subt < 0) {
  104. if (types != null)
  105. result = search_in_array (types, className, ignoreCase);
  106. } else {
  107. string pname, rname;
  108. pname = className.Substring (0, subt);
  109. rname = className.Substring (subt + 1);
  110. result = search_in_array (types, pname, ignoreCase);
  111. if ((result != null) && (result.subtypes != null))
  112. result = search_in_array (result.subtypes, rname, ignoreCase);
  113. else
  114. result = null;
  115. }
  116. if ((result == null) && throwOnError)
  117. throw new TypeLoadException (className);
  118. if (result != null && (isbyref || arrayrank > 0))
  119. return create_modified_type (result, arrayrank, isbyref);
  120. return result;
  121. }
  122. internal int get_next_table_index (int table, bool inc) {
  123. return assemblyb.get_next_table_index (table, inc);
  124. }
  125. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  126. if (cattrs != null) {
  127. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  128. cattrs.CopyTo (new_array, 0);
  129. new_array [cattrs.Length] = customBuilder;
  130. cattrs = new_array;
  131. } else {
  132. cattrs = new CustomAttributeBuilder [1];
  133. cattrs [0] = customBuilder;
  134. }
  135. }
  136. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  137. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  138. }
  139. }
  140. }