MethodBuilder.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // System.Reflection.Emit/MethodBuilder.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.Reflection.Emit;
  12. using System.Globalization;
  13. using System.Runtime.CompilerServices;
  14. using System.Runtime.InteropServices;
  15. namespace System.Reflection.Emit {
  16. public sealed class MethodBuilder : MethodInfo {
  17. private RuntimeMethodHandle mhandle;
  18. private Type rtype;
  19. private Type[] parameters;
  20. private MethodAttributes attrs;
  21. private MethodImplAttributes iattrs;
  22. private string name;
  23. private int table_idx;
  24. private byte[] code;
  25. private ILGenerator ilgen;
  26. private TypeBuilder type;
  27. private ParameterBuilder[] pinfo;
  28. private CustomAttributeBuilder[] cattrs;
  29. private MethodInfo override_method;
  30. private string pi_dll;
  31. private string pi_entry;
  32. private CharSet ncharset;
  33. private CallingConvention native_cc;
  34. private CallingConventions call_conv;
  35. private bool init_locals = true;
  36. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  37. this.name = name;
  38. this.attrs = attributes;
  39. this.call_conv = callingConvention;
  40. this.rtype = returnType;
  41. if (parameterTypes != null) {
  42. this.parameters = new Type [parameterTypes.Length];
  43. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  44. }
  45. type = tb;
  46. table_idx = get_next_table_index (this, 0x06, true);
  47. //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
  48. }
  49. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
  50. CallingConventions callingConvention, Type returnType, Type[] parameterTypes,
  51. String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
  52. : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
  53. pi_dll = dllName;
  54. pi_entry = entryName;
  55. native_cc = nativeCConv;
  56. ncharset = nativeCharset;
  57. }
  58. public bool InitLocals {
  59. get {return init_locals;}
  60. set {init_locals = value;}
  61. }
  62. internal TypeBuilder TypeBuilder {
  63. get {return type;}
  64. }
  65. public override Type ReturnType {get {return rtype;}}
  66. public override Type ReflectedType {get {return type;}}
  67. public override Type DeclaringType {get {return type;}}
  68. public override string Name {get {return name;}}
  69. public override RuntimeMethodHandle MethodHandle {get {return mhandle;}}
  70. public override MethodAttributes Attributes {get {return attrs;}}
  71. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  72. get {return null;}
  73. }
  74. public MethodToken GetToken() {
  75. return new MethodToken(0x06000000 | table_idx);
  76. }
  77. public override MethodInfo GetBaseDefinition() {
  78. return this;
  79. }
  80. public override MethodImplAttributes GetMethodImplementationFlags() {
  81. return iattrs;
  82. }
  83. public override ParameterInfo[] GetParameters() {
  84. if (parameters == null)
  85. return null;
  86. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  87. for (int i = 0; i < parameters.Length; i++) {
  88. retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
  89. }
  90. return retval;
  91. }
  92. public void CreateMethodBody( byte[] il, int count) {
  93. code = new byte [count];
  94. System.Array.Copy(il, code, count);
  95. }
  96. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  97. return null;
  98. }
  99. public override bool IsDefined (Type attribute_type, bool inherit) {
  100. return false;
  101. }
  102. public override object[] GetCustomAttributes( bool inherit) {
  103. return null;
  104. }
  105. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  106. return null;
  107. }
  108. public ILGenerator GetILGenerator () {
  109. return GetILGenerator (64);
  110. }
  111. public ILGenerator GetILGenerator (int size) {
  112. ilgen = new ILGenerator (this, size);
  113. return ilgen;
  114. }
  115. [MonoTODO]
  116. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  117. {
  118. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  119. // check position
  120. if (pinfo == null)
  121. pinfo = new ParameterBuilder [parameters.Length + 1];
  122. pinfo [position] = pb;
  123. return pb;
  124. }
  125. internal void fixup () {
  126. if (ilgen != null)
  127. ilgen.label_fixup ();
  128. }
  129. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  130. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  131. if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
  132. byte[] data = customBuilder.Data;
  133. int impla; // the (stupid) ctor takes a short or an int ...
  134. impla = (int)data [2];
  135. impla |= ((int)data [3]) << 8;
  136. SetImplementationFlags ((MethodImplAttributes)impla);
  137. return;
  138. }
  139. if (cattrs != null) {
  140. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  141. cattrs.CopyTo (new_array, 0);
  142. new_array [cattrs.Length] = customBuilder;
  143. cattrs = new_array;
  144. } else {
  145. cattrs = new CustomAttributeBuilder [1];
  146. cattrs [0] = customBuilder;
  147. }
  148. }
  149. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  150. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  151. }
  152. public void SetImplementationFlags( MethodImplAttributes attributes) {
  153. iattrs = attributes;
  154. }
  155. internal override int get_next_table_index (object obj, int table, bool inc) {
  156. return type.get_next_table_index (obj, table, inc);
  157. }
  158. internal void set_override (MethodInfo mdecl) {
  159. override_method = mdecl;
  160. }
  161. }
  162. }