MethodBuilder.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes) {
  36. this.name = name;
  37. this.attrs = attributes;
  38. this.call_conv = callingConvention;
  39. this.rtype = returnType;
  40. if (parameterTypes != null) {
  41. this.parameters = new Type [parameterTypes.Length];
  42. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  43. }
  44. type = tb;
  45. table_idx = get_next_table_index (0x06, true);
  46. //Console.WriteLine ("index for "+name+" set to "+table_idx.ToString());
  47. }
  48. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
  49. CallingConventions callingConvention, Type returnType, Type[] parameterTypes,
  50. String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
  51. : this (tb, name, attributes, callingConvention, returnType, parameterTypes) {
  52. pi_dll = dllName;
  53. pi_entry = entryName;
  54. native_cc = nativeCConv;
  55. ncharset = nativeCharset;
  56. }
  57. internal TypeBuilder TypeBuilder {
  58. get {return type;}
  59. }
  60. public override Type ReturnType {get {return rtype;}}
  61. public override Type ReflectedType {get {return type;}}
  62. public override Type DeclaringType {get {return type;}}
  63. public override string Name {get {return name;}}
  64. public override RuntimeMethodHandle MethodHandle {get {return mhandle;}}
  65. public override MethodAttributes Attributes {get {return attrs;}}
  66. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  67. get {return null;}
  68. }
  69. public MethodToken GetToken() {
  70. return new MethodToken(0x06000000 | table_idx);
  71. }
  72. public override MethodInfo GetBaseDefinition() {
  73. return null;
  74. }
  75. public override MethodImplAttributes GetMethodImplementationFlags() {
  76. return iattrs;
  77. }
  78. public override ParameterInfo[] GetParameters() {
  79. return null;
  80. }
  81. public void CreateMethodBody( byte[] il, int count) {
  82. code = new byte [count];
  83. System.Array.Copy(il, code, count);
  84. }
  85. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  86. return null;
  87. }
  88. public override bool IsDefined (Type attribute_type, bool inherit) {
  89. return false;
  90. }
  91. public override object[] GetCustomAttributes( bool inherit) {
  92. return null;
  93. }
  94. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  95. return null;
  96. }
  97. public ILGenerator GetILGenerator () {
  98. return GetILGenerator (256);
  99. }
  100. public ILGenerator GetILGenerator (int size) {
  101. ilgen = new ILGenerator (this, size);
  102. return ilgen;
  103. }
  104. [MonoTODO]
  105. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  106. {
  107. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  108. // check position
  109. if (pinfo == null)
  110. pinfo = new ParameterBuilder [parameters.Length + 1];
  111. pinfo [position] = pb;
  112. return pb;
  113. }
  114. internal void fixup () {
  115. if (ilgen != null)
  116. ilgen.label_fixup ();
  117. }
  118. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  119. if (cattrs != null) {
  120. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  121. cattrs.CopyTo (new_array, 0);
  122. new_array [cattrs.Length] = customBuilder;
  123. cattrs = new_array;
  124. } else {
  125. cattrs = new CustomAttributeBuilder [1];
  126. cattrs [0] = customBuilder;
  127. }
  128. }
  129. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  130. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  131. }
  132. public void SetImplementationFlags( MethodImplAttributes attributes) {
  133. iattrs = attributes;
  134. }
  135. internal override int get_next_table_index (int table, bool inc) {
  136. return type.get_next_table_index (table, inc);
  137. }
  138. internal void set_override (MethodInfo mdecl) {
  139. override_method = mdecl;
  140. }
  141. }
  142. }