MonoMethod.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Globalization;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.InteropServices;
  36. using System.Runtime.Serialization;
  37. namespace System.Reflection {
  38. internal struct MonoMethodInfo
  39. {
  40. internal Type parent;
  41. internal Type ret;
  42. internal MethodAttributes attrs;
  43. internal MethodImplAttributes iattrs;
  44. internal CallingConventions callconv;
  45. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  46. internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
  47. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  48. internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
  49. };
  50. /*
  51. * Note: most of this class needs to be duplicated for the contructor, since
  52. * the .NET reflection class hierarchy is so broken.
  53. */
  54. [Serializable()]
  55. internal class MonoMethod : MethodInfo, ISerializable
  56. {
  57. internal IntPtr mhandle;
  58. string name;
  59. Type reftype;
  60. internal MonoMethod () {
  61. }
  62. internal MonoMethod (RuntimeMethodHandle mhandle) {
  63. this.mhandle = mhandle.Value;
  64. }
  65. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  66. internal static extern MonoMethod get_base_definition (MonoMethod method);
  67. public override MethodInfo GetBaseDefinition ()
  68. {
  69. return get_base_definition (this);
  70. }
  71. public override Type ReturnType {
  72. get {
  73. MonoMethodInfo info;
  74. MonoMethodInfo.get_method_info (mhandle, out info);
  75. return info.ret;
  76. }
  77. }
  78. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  79. get {
  80. return new ParameterInfo (ReturnType, this);
  81. }
  82. }
  83. public override MethodImplAttributes GetMethodImplementationFlags() {
  84. MonoMethodInfo info;
  85. MonoMethodInfo.get_method_info (mhandle, out info);
  86. return info.iattrs;
  87. }
  88. public override ParameterInfo[] GetParameters() {
  89. return MonoMethodInfo.get_parameter_info (mhandle);
  90. }
  91. /*
  92. * InternalInvoke() receives the parameters corretcly converted by the binder
  93. * to match the types of the method signature.
  94. */
  95. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  96. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  97. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  98. if (binder == null)
  99. binder = Binder.DefaultBinder;
  100. ParameterInfo[] pinfo = GetParameters ();
  101. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  102. throw new ArgumentException ("parameters");
  103. try {
  104. return InternalInvoke (obj, parameters);
  105. } catch (TargetException) {
  106. throw;
  107. } catch (Exception e) {
  108. throw new TargetInvocationException (e);
  109. }
  110. }
  111. public override RuntimeMethodHandle MethodHandle {
  112. get {return new RuntimeMethodHandle (mhandle);}
  113. }
  114. public override MethodAttributes Attributes {
  115. get {
  116. MonoMethodInfo info;
  117. MonoMethodInfo.get_method_info (mhandle, out info);
  118. return info.attrs;
  119. }
  120. }
  121. public override CallingConventions CallingConvention {
  122. get {
  123. MonoMethodInfo info;
  124. MonoMethodInfo.get_method_info (mhandle, out info);
  125. return info.callconv;
  126. }
  127. }
  128. public override Type ReflectedType {
  129. get {
  130. return reftype;
  131. }
  132. }
  133. public override Type DeclaringType {
  134. get {
  135. MonoMethodInfo info;
  136. MonoMethodInfo.get_method_info (mhandle, out info);
  137. return info.parent;
  138. }
  139. }
  140. public override string Name {
  141. get {
  142. return name;
  143. }
  144. }
  145. public override bool IsDefined (Type attributeType, bool inherit) {
  146. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  147. }
  148. public override object[] GetCustomAttributes( bool inherit) {
  149. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  150. }
  151. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  152. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  153. }
  154. public override string ToString () {
  155. string parms = "";
  156. ParameterInfo[] p = GetParameters ();
  157. for (int i = 0; i < p.Length; ++i) {
  158. if (i > 0)
  159. parms = parms + ", ";
  160. Type pt = p[i].ParameterType;
  161. if (pt.IsClass && pt.Namespace != "")
  162. parms = parms + pt.Namespace + "." + pt.Name;
  163. else
  164. parms = parms + pt.Name;
  165. }
  166. if (ReturnType.IsClass && ReturnType.Namespace != "")
  167. return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
  168. string generic = "";
  169. #if NET_2_0 || BOOTSTRAP_NET_2_0
  170. if (HasGenericParameters) {
  171. Type[] gen_params = GetGenericArguments ();
  172. generic = "[";
  173. for (int j = 0; j < gen_params.Length; j++) {
  174. if (j > 0)
  175. generic += ",";
  176. generic += gen_params [j].Name;
  177. }
  178. generic += "]";
  179. }
  180. #endif
  181. return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
  182. }
  183. // ISerializable
  184. public void GetObjectData(SerializationInfo info, StreamingContext context)
  185. {
  186. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  187. }
  188. #if NET_2_0 || BOOTSTRAP_NET_2_0
  189. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  190. public override extern MethodInfo BindGenericParameters (Type [] types);
  191. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  192. public override extern Type [] GetGenericArguments ();
  193. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  194. extern MethodInfo GetGenericMethodDefinition_impl ();
  195. public override MethodInfo GetGenericMethodDefinition ()
  196. {
  197. MethodInfo res = GetGenericMethodDefinition_impl ();
  198. if (res == null)
  199. throw new InvalidOperationException ();
  200. return res;
  201. }
  202. public override extern bool Mono_IsInflatedMethod {
  203. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  204. get;
  205. }
  206. public override extern bool HasGenericParameters {
  207. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  208. get;
  209. }
  210. public override extern bool IsGenericMethodDefinition {
  211. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  212. get;
  213. }
  214. #endif
  215. }
  216. internal class MonoCMethod : ConstructorInfo, ISerializable
  217. {
  218. internal IntPtr mhandle;
  219. string name;
  220. Type reftype;
  221. public override MethodImplAttributes GetMethodImplementationFlags() {
  222. MonoMethodInfo info;
  223. MonoMethodInfo.get_method_info (mhandle, out info);
  224. return info.iattrs;
  225. }
  226. public override ParameterInfo[] GetParameters() {
  227. return MonoMethodInfo.get_parameter_info (mhandle);
  228. }
  229. /*
  230. * InternalInvoke() receives the parameters corretcly converted by the binder
  231. * to match the types of the method signature.
  232. */
  233. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  234. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  235. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  236. if (binder == null)
  237. binder = Binder.DefaultBinder;
  238. ParameterInfo[] pinfo = GetParameters ();
  239. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  240. throw new ArgumentException ("parameters");
  241. try {
  242. return InternalInvoke (obj, parameters);
  243. } catch (TargetException) {
  244. throw;
  245. } catch (Exception e) {
  246. throw new TargetInvocationException (e);
  247. }
  248. }
  249. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  250. return Invoke (null, invokeAttr, binder, parameters, culture);
  251. }
  252. public override RuntimeMethodHandle MethodHandle {
  253. get {return new RuntimeMethodHandle (mhandle);}
  254. }
  255. public override MethodAttributes Attributes {
  256. get {
  257. MonoMethodInfo info;
  258. MonoMethodInfo.get_method_info (mhandle, out info);
  259. return info.attrs;
  260. }
  261. }
  262. public override CallingConventions CallingConvention {
  263. get {
  264. MonoMethodInfo info;
  265. MonoMethodInfo.get_method_info (mhandle, out info);
  266. return info.callconv;
  267. }
  268. }
  269. public override Type ReflectedType {
  270. get {
  271. return reftype;
  272. }
  273. }
  274. public override Type DeclaringType {
  275. get {
  276. MonoMethodInfo info;
  277. MonoMethodInfo.get_method_info (mhandle, out info);
  278. return info.parent;
  279. }
  280. }
  281. public override string Name {
  282. get {
  283. return name;
  284. }
  285. }
  286. public override bool IsDefined (Type attributeType, bool inherit) {
  287. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  288. }
  289. public override object[] GetCustomAttributes( bool inherit) {
  290. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  291. }
  292. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  293. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  294. }
  295. #if NET_2_0 || BOOTSTRAP_NET_2_0
  296. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  297. extern MethodInfo GetGenericMethodDefinition_impl ();
  298. public override MethodInfo GetGenericMethodDefinition ()
  299. {
  300. MethodInfo res = GetGenericMethodDefinition_impl ();
  301. if (res == null)
  302. throw new InvalidOperationException ();
  303. return res;
  304. }
  305. public override extern bool Mono_IsInflatedMethod {
  306. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  307. get;
  308. }
  309. public override bool HasGenericParameters {
  310. get {
  311. return false;
  312. }
  313. }
  314. public override bool IsGenericMethodDefinition {
  315. get {
  316. return false;
  317. }
  318. }
  319. #endif
  320. public override string ToString () {
  321. string parms = "";
  322. ParameterInfo[] p = GetParameters ();
  323. for (int i = 0; i < p.Length; ++i) {
  324. if (i > 0)
  325. parms = parms + ", ";
  326. parms = parms + p [i].ParameterType.Name;
  327. }
  328. return "Void "+Name+"("+parms+")";
  329. }
  330. // ISerializable
  331. public void GetObjectData(SerializationInfo info, StreamingContext context)
  332. {
  333. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  334. }
  335. }
  336. }