MonoMethod.cs 8.0 KB

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