MonoMethod.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.Reflection/MonoMethod.cs
  3. // The class used to represent methods from the mono runtime.
  4. //
  5. // Author:
  6. // Paolo Molaro ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Globalization;
  12. using System.Runtime.CompilerServices;
  13. using System.Runtime.InteropServices;
  14. namespace System.Reflection {
  15. internal struct MonoMethodInfo {
  16. internal Type parent;
  17. internal Type ret;
  18. internal MethodAttributes attrs;
  19. internal MethodImplAttributes iattrs;
  20. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  21. internal static extern void get_method_info (RuntimeMethodHandle handle, out MonoMethodInfo info);
  22. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  23. internal static extern ParameterInfo[] get_parameter_info (RuntimeMethodHandle handle);
  24. };
  25. /*
  26. * Note: most of this class needs to be duplicated for the contructor, since
  27. * the .NET reflection class hierarchy is so broken.
  28. */
  29. internal class MonoMethod : MethodInfo {
  30. internal RuntimeMethodHandle mhandle;
  31. string name;
  32. Type reftype;
  33. [MonoTODO]
  34. public override MethodInfo GetBaseDefinition() {
  35. return this; /* FIXME */
  36. }
  37. public override Type ReturnType {
  38. get {
  39. MonoMethodInfo info;
  40. MonoMethodInfo.get_method_info (mhandle, out info);
  41. return info.ret;
  42. }
  43. }
  44. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  45. get {return null;}
  46. }
  47. public override MethodImplAttributes GetMethodImplementationFlags() {
  48. MonoMethodInfo info;
  49. MonoMethodInfo.get_method_info (mhandle, out info);
  50. return info.iattrs;
  51. }
  52. public override ParameterInfo[] GetParameters() {
  53. return MonoMethodInfo.get_parameter_info (mhandle);
  54. }
  55. /*
  56. * InternalInvoke() receives the parameters corretcly converted by the binder
  57. * to match the types of the method signature.
  58. */
  59. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  60. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  61. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  62. if (binder == null)
  63. binder = Binder.DefaultBinder;
  64. ParameterInfo[] pinfo = GetParameters ();
  65. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  66. throw new ArgumentException ("parameters");
  67. try {
  68. return InternalInvoke (obj, parameters);
  69. } catch (Exception e) {
  70. throw new TargetInvocationException (e);
  71. }
  72. }
  73. public override RuntimeMethodHandle MethodHandle {
  74. get {return mhandle;}
  75. }
  76. public override MethodAttributes Attributes {
  77. get {
  78. MonoMethodInfo info;
  79. MonoMethodInfo.get_method_info (mhandle, out info);
  80. return info.attrs;
  81. }
  82. }
  83. public override Type ReflectedType {
  84. get {
  85. return reftype;
  86. }
  87. }
  88. public override Type DeclaringType {
  89. get {
  90. MonoMethodInfo info;
  91. MonoMethodInfo.get_method_info (mhandle, out info);
  92. return info.parent;
  93. }
  94. }
  95. public override string Name {
  96. get {
  97. return name;
  98. }
  99. }
  100. public override bool IsDefined (Type attributeType, bool inherit) {
  101. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  102. }
  103. public override object[] GetCustomAttributes( bool inherit) {
  104. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  105. }
  106. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  107. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  108. }
  109. public override string ToString () {
  110. string parms = "";
  111. ParameterInfo[] p = GetParameters ();
  112. for (int i = 0; i < p.Length; ++i) {
  113. if (i > 0)
  114. parms = parms + ", ";
  115. parms = parms + p [i].ParameterType.Name;
  116. }
  117. return ReturnType.Name+" "+Name+"("+parms+")";
  118. }
  119. }
  120. internal class MonoCMethod : ConstructorInfo {
  121. internal RuntimeMethodHandle mhandle;
  122. string name;
  123. Type reftype;
  124. public override MethodImplAttributes GetMethodImplementationFlags() {
  125. MonoMethodInfo info;
  126. MonoMethodInfo.get_method_info (mhandle, out info);
  127. return info.iattrs;
  128. }
  129. public override ParameterInfo[] GetParameters() {
  130. return MonoMethodInfo.get_parameter_info (mhandle);
  131. }
  132. /*
  133. * InternalInvoke() receives the parameters corretcly converted by the binder
  134. * to match the types of the method signature.
  135. */
  136. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  137. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  138. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  139. if (binder == null)
  140. binder = Binder.DefaultBinder;
  141. ParameterInfo[] pinfo = GetParameters ();
  142. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  143. throw new ArgumentException ("parameters");
  144. try {
  145. return InternalInvoke (obj, parameters);
  146. } catch (Exception e) {
  147. throw new TargetInvocationException (e);
  148. }
  149. }
  150. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  151. return Invoke (null, invokeAttr, binder, parameters, culture);
  152. }
  153. public override RuntimeMethodHandle MethodHandle {
  154. get {return mhandle;}
  155. }
  156. public override MethodAttributes Attributes {
  157. get {
  158. MonoMethodInfo info;
  159. MonoMethodInfo.get_method_info (mhandle, out info);
  160. return info.attrs;
  161. }
  162. }
  163. public override Type ReflectedType {
  164. get {
  165. return reftype;
  166. }
  167. }
  168. public override Type DeclaringType {
  169. get {
  170. MonoMethodInfo info;
  171. MonoMethodInfo.get_method_info (mhandle, out info);
  172. return info.parent;
  173. }
  174. }
  175. public override string Name {
  176. get {
  177. return name;
  178. }
  179. }
  180. public override bool IsDefined (Type attributeType, bool inherit) {
  181. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  182. }
  183. public override object[] GetCustomAttributes( bool inherit) {
  184. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  185. }
  186. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  187. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  188. }
  189. public override string ToString () {
  190. string parms = "";
  191. ParameterInfo[] p = GetParameters ();
  192. for (int i = 0; i < p.Length; ++i) {
  193. if (i > 0)
  194. parms = parms + ", ";
  195. parms = parms + p [i].ParameterType.Name;
  196. }
  197. return "Void "+Name+"("+parms+")";
  198. }
  199. }
  200. }