2
0

MethodBuilder.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 null;
  79. }
  80. public override MethodImplAttributes GetMethodImplementationFlags() {
  81. return iattrs;
  82. }
  83. public override ParameterInfo[] GetParameters() {
  84. if ((parameters == null) || (pinfo == null))
  85. return null;
  86. ParameterInfo[] retval = new ParameterInfo [parameters.Length - 1];
  87. for (int i = 1; i < parameters.Length; i++) {
  88. if (pinfo [i] == null)
  89. return null;
  90. retval [i - 1] = new ParameterInfo (pinfo [i], parameters [i - 1], this);
  91. }
  92. return retval;
  93. }
  94. public void CreateMethodBody( byte[] il, int count) {
  95. code = new byte [count];
  96. System.Array.Copy(il, code, count);
  97. }
  98. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  99. return null;
  100. }
  101. public override bool IsDefined (Type attribute_type, bool inherit) {
  102. return false;
  103. }
  104. public override object[] GetCustomAttributes( bool inherit) {
  105. return null;
  106. }
  107. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  108. return null;
  109. }
  110. public ILGenerator GetILGenerator () {
  111. return GetILGenerator (256);
  112. }
  113. public ILGenerator GetILGenerator (int size) {
  114. ilgen = new ILGenerator (this, size);
  115. return ilgen;
  116. }
  117. [MonoTODO]
  118. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  119. {
  120. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  121. // check position
  122. if (pinfo == null)
  123. pinfo = new ParameterBuilder [parameters.Length + 1];
  124. pinfo [position] = pb;
  125. return pb;
  126. }
  127. internal void fixup () {
  128. if (ilgen != null)
  129. ilgen.label_fixup ();
  130. }
  131. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  132. if (cattrs != null) {
  133. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  134. cattrs.CopyTo (new_array, 0);
  135. new_array [cattrs.Length] = customBuilder;
  136. cattrs = new_array;
  137. } else {
  138. cattrs = new CustomAttributeBuilder [1];
  139. cattrs [0] = customBuilder;
  140. }
  141. }
  142. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  143. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  144. }
  145. public void SetImplementationFlags( MethodImplAttributes attributes) {
  146. iattrs = attributes;
  147. }
  148. internal override int get_next_table_index (object obj, int table, bool inc) {
  149. return type.get_next_table_index (obj, table, inc);
  150. }
  151. internal void set_override (MethodInfo mdecl) {
  152. override_method = mdecl;
  153. }
  154. }
  155. }