MonoMethod.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. Type pt = p[i].ParameterType;
  139. if (pt.IsClass && pt.Namespace != "")
  140. parms = parms + pt.Namespace + "." + pt.Name;
  141. else
  142. parms = parms + pt.Name;
  143. }
  144. if (ReturnType.IsClass && ReturnType.Namespace != "")
  145. return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
  146. string generic = "";
  147. #if NET_2_0 | BOOTSTRAP_NET_2_0
  148. if (HasGenericParameters) {
  149. Type[] gen_params = GetGenericArguments ();
  150. generic = "[";
  151. for (int j = 0; j < gen_params.Length; j++) {
  152. if (j > 0)
  153. generic += ",";
  154. generic += gen_params [j].Name;
  155. }
  156. generic += "]";
  157. }
  158. #endif
  159. return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
  160. }
  161. // ISerializable
  162. public void GetObjectData(SerializationInfo info, StreamingContext context)
  163. {
  164. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  165. }
  166. #if NET_2_0 | BOOTSTRAP_NET_2_0
  167. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  168. public override extern MethodInfo BindGenericParameters (Type [] types);
  169. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  170. public override extern Type [] GetGenericArguments ();
  171. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  172. extern MethodInfo GetGenericMethodDefinition_impl ();
  173. public override MethodInfo GetGenericMethodDefinition ()
  174. {
  175. MethodInfo res = GetGenericMethodDefinition_impl ();
  176. if (res == null)
  177. throw new InvalidOperationException ();
  178. return res;
  179. }
  180. public override extern bool Mono_IsInflatedMethod {
  181. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  182. get;
  183. }
  184. public override extern bool HasGenericParameters {
  185. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  186. get;
  187. }
  188. public override extern bool IsGenericMethodDefinition {
  189. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  190. get;
  191. }
  192. #endif
  193. }
  194. internal class MonoCMethod : ConstructorInfo, ISerializable
  195. {
  196. internal IntPtr mhandle;
  197. string name;
  198. Type reftype;
  199. public override MethodImplAttributes GetMethodImplementationFlags() {
  200. MonoMethodInfo info;
  201. MonoMethodInfo.get_method_info (mhandle, out info);
  202. return info.iattrs;
  203. }
  204. public override ParameterInfo[] GetParameters() {
  205. return MonoMethodInfo.get_parameter_info (mhandle);
  206. }
  207. /*
  208. * InternalInvoke() receives the parameters corretcly converted by the binder
  209. * to match the types of the method signature.
  210. */
  211. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  212. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  213. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  214. if (binder == null)
  215. binder = Binder.DefaultBinder;
  216. ParameterInfo[] pinfo = GetParameters ();
  217. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  218. throw new ArgumentException ("parameters");
  219. try {
  220. return InternalInvoke (obj, parameters);
  221. } catch (TargetException) {
  222. throw;
  223. } catch (Exception e) {
  224. throw new TargetInvocationException (e);
  225. }
  226. }
  227. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  228. return Invoke (null, invokeAttr, binder, parameters, culture);
  229. }
  230. public override RuntimeMethodHandle MethodHandle {
  231. get {return new RuntimeMethodHandle (mhandle);}
  232. }
  233. public override MethodAttributes Attributes {
  234. get {
  235. MonoMethodInfo info;
  236. MonoMethodInfo.get_method_info (mhandle, out info);
  237. return info.attrs;
  238. }
  239. }
  240. public override CallingConventions CallingConvention {
  241. get {
  242. MonoMethodInfo info;
  243. MonoMethodInfo.get_method_info (mhandle, out info);
  244. return info.callconv;
  245. }
  246. }
  247. public override Type ReflectedType {
  248. get {
  249. return reftype;
  250. }
  251. }
  252. public override Type DeclaringType {
  253. get {
  254. MonoMethodInfo info;
  255. MonoMethodInfo.get_method_info (mhandle, out info);
  256. return info.parent;
  257. }
  258. }
  259. public override string Name {
  260. get {
  261. return name;
  262. }
  263. }
  264. public override bool IsDefined (Type attributeType, bool inherit) {
  265. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  266. }
  267. public override object[] GetCustomAttributes( bool inherit) {
  268. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  269. }
  270. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  271. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  272. }
  273. #if NET_2_0 | BOOTSTRAP_NET_2_0
  274. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  275. extern MethodInfo GetGenericMethodDefinition_impl ();
  276. public override MethodInfo GetGenericMethodDefinition ()
  277. {
  278. MethodInfo res = GetGenericMethodDefinition_impl ();
  279. if (res == null)
  280. throw new InvalidOperationException ();
  281. return res;
  282. }
  283. public override extern bool Mono_IsInflatedMethod {
  284. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  285. get;
  286. }
  287. public override bool HasGenericParameters {
  288. get {
  289. return false;
  290. }
  291. }
  292. public override bool IsGenericMethodDefinition {
  293. get {
  294. return false;
  295. }
  296. }
  297. #endif
  298. public override string ToString () {
  299. string parms = "";
  300. ParameterInfo[] p = GetParameters ();
  301. for (int i = 0; i < p.Length; ++i) {
  302. if (i > 0)
  303. parms = parms + ", ";
  304. parms = parms + p [i].ParameterType.Name;
  305. }
  306. return "Void "+Name+"("+parms+")";
  307. }
  308. // ISerializable
  309. public void GetObjectData(SerializationInfo info, StreamingContext context)
  310. {
  311. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  312. }
  313. }
  314. }