MonoMethod.cs 7.8 KB

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