MonoMethod.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 (IntPtr handle, out MonoMethodInfo info);
  22. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  23. internal static extern ParameterInfo[] get_parameter_info (IntPtr 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 IntPtr 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 {
  46. return new ParameterInfo (ReturnType, this);
  47. }
  48. }
  49. public override MethodImplAttributes GetMethodImplementationFlags() {
  50. MonoMethodInfo info;
  51. MonoMethodInfo.get_method_info (mhandle, out info);
  52. return info.iattrs;
  53. }
  54. public override ParameterInfo[] GetParameters() {
  55. return MonoMethodInfo.get_parameter_info (mhandle);
  56. }
  57. /*
  58. * InternalInvoke() receives the parameters corretcly converted by the binder
  59. * to match the types of the method signature.
  60. */
  61. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  62. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  63. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  64. if (binder == null)
  65. binder = Binder.DefaultBinder;
  66. ParameterInfo[] pinfo = GetParameters ();
  67. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  68. throw new ArgumentException ("parameters");
  69. try {
  70. return InternalInvoke (obj, parameters);
  71. } catch (TargetException e) {
  72. throw;
  73. } catch (Exception e) {
  74. throw new TargetInvocationException (e);
  75. }
  76. }
  77. public override RuntimeMethodHandle MethodHandle {
  78. get {return new RuntimeMethodHandle (mhandle);}
  79. }
  80. public override MethodAttributes Attributes {
  81. get {
  82. MonoMethodInfo info;
  83. MonoMethodInfo.get_method_info (mhandle, out info);
  84. return info.attrs;
  85. }
  86. }
  87. public override Type ReflectedType {
  88. get {
  89. return reftype;
  90. }
  91. }
  92. public override Type DeclaringType {
  93. get {
  94. MonoMethodInfo info;
  95. MonoMethodInfo.get_method_info (mhandle, out info);
  96. return info.parent;
  97. }
  98. }
  99. public override string Name {
  100. get {
  101. return name;
  102. }
  103. }
  104. public override bool IsDefined (Type attributeType, bool inherit) {
  105. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  106. }
  107. public override object[] GetCustomAttributes( bool inherit) {
  108. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  109. }
  110. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  111. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  112. }
  113. public override string ToString () {
  114. string parms = "";
  115. ParameterInfo[] p = GetParameters ();
  116. for (int i = 0; i < p.Length; ++i) {
  117. if (i > 0)
  118. parms = parms + ", ";
  119. parms = parms + p [i].ParameterType.Name;
  120. }
  121. return ReturnType.Name+" "+Name+"("+parms+")";
  122. }
  123. }
  124. internal class MonoCMethod : ConstructorInfo {
  125. internal IntPtr mhandle;
  126. string name;
  127. Type reftype;
  128. public override MethodImplAttributes GetMethodImplementationFlags() {
  129. MonoMethodInfo info;
  130. MonoMethodInfo.get_method_info (mhandle, out info);
  131. return info.iattrs;
  132. }
  133. public override ParameterInfo[] GetParameters() {
  134. return MonoMethodInfo.get_parameter_info (mhandle);
  135. }
  136. /*
  137. * InternalInvoke() receives the parameters corretcly converted by the binder
  138. * to match the types of the method signature.
  139. */
  140. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  141. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  142. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  143. if (binder == null)
  144. binder = Binder.DefaultBinder;
  145. ParameterInfo[] pinfo = GetParameters ();
  146. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  147. throw new ArgumentException ("parameters");
  148. try {
  149. return InternalInvoke (obj, parameters);
  150. } catch (TargetException e) {
  151. throw;
  152. } catch (Exception e) {
  153. throw new TargetInvocationException (e);
  154. }
  155. }
  156. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  157. return Invoke (null, invokeAttr, binder, parameters, culture);
  158. }
  159. public override RuntimeMethodHandle MethodHandle {
  160. get {return new RuntimeMethodHandle (mhandle);}
  161. }
  162. public override MethodAttributes Attributes {
  163. get {
  164. MonoMethodInfo info;
  165. MonoMethodInfo.get_method_info (mhandle, out info);
  166. return info.attrs;
  167. }
  168. }
  169. public override Type ReflectedType {
  170. get {
  171. return reftype;
  172. }
  173. }
  174. public override Type DeclaringType {
  175. get {
  176. MonoMethodInfo info;
  177. MonoMethodInfo.get_method_info (mhandle, out info);
  178. return info.parent;
  179. }
  180. }
  181. public override string Name {
  182. get {
  183. return name;
  184. }
  185. }
  186. public override bool IsDefined (Type attributeType, bool inherit) {
  187. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  188. }
  189. public override object[] GetCustomAttributes( bool inherit) {
  190. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  191. }
  192. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  193. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  194. }
  195. public override string ToString () {
  196. string parms = "";
  197. ParameterInfo[] p = GetParameters ();
  198. for (int i = 0; i < p.Length; ++i) {
  199. if (i > 0)
  200. parms = parms + ", ";
  201. parms = parms + p [i].ParameterType.Name;
  202. }
  203. return "Void "+Name+"("+parms+")";
  204. }
  205. }
  206. }