MonoMethod.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. using System.Runtime.Serialization;
  15. namespace System.Reflection {
  16. internal struct MonoMethodInfo
  17. {
  18. internal Type parent;
  19. internal Type ret;
  20. internal MethodAttributes attrs;
  21. internal MethodImplAttributes iattrs;
  22. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  23. internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
  24. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  25. internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
  26. };
  27. /*
  28. * Note: most of this class needs to be duplicated for the contructor, since
  29. * the .NET reflection class hierarchy is so broken.
  30. */
  31. [Serializable()]
  32. internal class MonoMethod : MethodInfo, ISerializable
  33. {
  34. internal IntPtr mhandle;
  35. string name;
  36. Type reftype;
  37. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  38. internal static extern MonoMethod get_base_definition (MonoMethod method);
  39. public override MethodInfo GetBaseDefinition ()
  40. {
  41. return get_base_definition (this);
  42. }
  43. public override Type ReturnType {
  44. get {
  45. MonoMethodInfo info;
  46. MonoMethodInfo.get_method_info (mhandle, out info);
  47. return info.ret;
  48. }
  49. }
  50. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  51. get {
  52. return new ParameterInfo (ReturnType, this);
  53. }
  54. }
  55. public override MethodImplAttributes GetMethodImplementationFlags() {
  56. MonoMethodInfo info;
  57. MonoMethodInfo.get_method_info (mhandle, out info);
  58. return info.iattrs;
  59. }
  60. public override ParameterInfo[] GetParameters() {
  61. return MonoMethodInfo.get_parameter_info (mhandle);
  62. }
  63. /*
  64. * InternalInvoke() receives the parameters corretcly converted by the binder
  65. * to match the types of the method signature.
  66. */
  67. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  68. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  69. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  70. if (binder == null)
  71. binder = Binder.DefaultBinder;
  72. ParameterInfo[] pinfo = GetParameters ();
  73. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  74. throw new ArgumentException ("parameters");
  75. try {
  76. return InternalInvoke (obj, parameters);
  77. } catch (TargetException) {
  78. throw;
  79. } catch (Exception e) {
  80. throw new TargetInvocationException (e);
  81. }
  82. }
  83. public override RuntimeMethodHandle MethodHandle {
  84. get {return new RuntimeMethodHandle (mhandle);}
  85. }
  86. public override MethodAttributes Attributes {
  87. get {
  88. MonoMethodInfo info;
  89. MonoMethodInfo.get_method_info (mhandle, out info);
  90. return info.attrs;
  91. }
  92. }
  93. public override Type ReflectedType {
  94. get {
  95. return reftype;
  96. }
  97. }
  98. public override Type DeclaringType {
  99. get {
  100. MonoMethodInfo info;
  101. MonoMethodInfo.get_method_info (mhandle, out info);
  102. return info.parent;
  103. }
  104. }
  105. public override string Name {
  106. get {
  107. return name;
  108. }
  109. }
  110. public override bool IsDefined (Type attributeType, bool inherit) {
  111. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  112. }
  113. public override object[] GetCustomAttributes( bool inherit) {
  114. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  115. }
  116. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  117. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  118. }
  119. public override string ToString () {
  120. string parms = "";
  121. ParameterInfo[] p = GetParameters ();
  122. for (int i = 0; i < p.Length; ++i) {
  123. if (i > 0)
  124. parms = parms + ", ";
  125. parms = parms + p [i].ParameterType.Name;
  126. }
  127. return ReturnType.Name+" "+Name+"("+parms+")";
  128. }
  129. // ISerializable
  130. public void GetObjectData(SerializationInfo info, StreamingContext context)
  131. {
  132. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  133. }
  134. }
  135. internal class MonoCMethod : ConstructorInfo, ISerializable
  136. {
  137. internal IntPtr mhandle;
  138. string name;
  139. Type reftype;
  140. public override MethodImplAttributes GetMethodImplementationFlags() {
  141. MonoMethodInfo info;
  142. MonoMethodInfo.get_method_info (mhandle, out info);
  143. return info.iattrs;
  144. }
  145. public override ParameterInfo[] GetParameters() {
  146. return MonoMethodInfo.get_parameter_info (mhandle);
  147. }
  148. /*
  149. * InternalInvoke() receives the parameters corretcly converted by the binder
  150. * to match the types of the method signature.
  151. */
  152. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  153. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  154. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  155. if (binder == null)
  156. binder = Binder.DefaultBinder;
  157. ParameterInfo[] pinfo = GetParameters ();
  158. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  159. throw new ArgumentException ("parameters");
  160. try {
  161. return InternalInvoke (obj, parameters);
  162. } catch (TargetException) {
  163. throw;
  164. } catch (Exception e) {
  165. throw new TargetInvocationException (e);
  166. }
  167. }
  168. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  169. return Invoke (null, invokeAttr, binder, parameters, culture);
  170. }
  171. public override RuntimeMethodHandle MethodHandle {
  172. get {return new RuntimeMethodHandle (mhandle);}
  173. }
  174. public override MethodAttributes Attributes {
  175. get {
  176. MonoMethodInfo info;
  177. MonoMethodInfo.get_method_info (mhandle, out info);
  178. return info.attrs;
  179. }
  180. }
  181. public override Type ReflectedType {
  182. get {
  183. return reftype;
  184. }
  185. }
  186. public override Type DeclaringType {
  187. get {
  188. MonoMethodInfo info;
  189. MonoMethodInfo.get_method_info (mhandle, out info);
  190. return info.parent;
  191. }
  192. }
  193. public override string Name {
  194. get {
  195. return name;
  196. }
  197. }
  198. public override bool IsDefined (Type attributeType, bool inherit) {
  199. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  200. }
  201. public override object[] GetCustomAttributes( bool inherit) {
  202. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  203. }
  204. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  205. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  206. }
  207. public override string ToString () {
  208. string parms = "";
  209. ParameterInfo[] p = GetParameters ();
  210. for (int i = 0; i < p.Length; ++i) {
  211. if (i > 0)
  212. parms = parms + ", ";
  213. parms = parms + p [i].ParameterType.Name;
  214. }
  215. return "Void "+Name+"("+parms+")";
  216. }
  217. // ISerializable
  218. public void GetObjectData(SerializationInfo info, StreamingContext context)
  219. {
  220. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  221. }
  222. }
  223. }