MonoMethod.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 correctly converted by the
  93. * binder 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. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  155. internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
  156. internal object[] GetPseudoCustomAttributes ()
  157. {
  158. int count = 0;
  159. /* MS.NET doesn't report MethodImplAttribute */
  160. MonoMethodInfo info;
  161. MonoMethodInfo.get_method_info (mhandle, out info);
  162. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  163. count ++;
  164. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
  165. count ++;
  166. if (count == 0)
  167. return null;
  168. object[] attrs = new object [count];
  169. count = 0;
  170. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  171. attrs [count ++] = new PreserveSigAttribute ();
  172. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
  173. DllImportAttribute attr = GetDllImportAttribute (mhandle);
  174. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  175. attr.PreserveSig = true;
  176. attrs [count ++] = attr;
  177. }
  178. return attrs;
  179. }
  180. public override string ToString () {
  181. string parms = "";
  182. ParameterInfo[] p = GetParameters ();
  183. for (int i = 0; i < p.Length; ++i) {
  184. if (i > 0)
  185. parms = parms + ", ";
  186. Type pt = p[i].ParameterType;
  187. if (pt.IsClass && pt.Namespace != "")
  188. parms = parms + pt.Namespace + "." + pt.Name;
  189. else
  190. parms = parms + pt.Name;
  191. }
  192. if (ReturnType.IsClass && ReturnType.Namespace != "")
  193. return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
  194. string generic = "";
  195. #if NET_2_0 || BOOTSTRAP_NET_2_0
  196. if (HasGenericParameters) {
  197. Type[] gen_params = GetGenericArguments ();
  198. generic = "[";
  199. for (int j = 0; j < gen_params.Length; j++) {
  200. if (j > 0)
  201. generic += ",";
  202. generic += gen_params [j].Name;
  203. }
  204. generic += "]";
  205. }
  206. #endif
  207. return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
  208. }
  209. // ISerializable
  210. public void GetObjectData(SerializationInfo info, StreamingContext context)
  211. {
  212. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  213. }
  214. #if NET_2_0 || BOOTSTRAP_NET_2_0
  215. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  216. public override extern MethodInfo BindGenericParameters (Type [] types);
  217. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  218. public override extern Type [] GetGenericArguments ();
  219. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  220. extern MethodInfo GetGenericMethodDefinition_impl ();
  221. public override MethodInfo GetGenericMethodDefinition ()
  222. {
  223. MethodInfo res = GetGenericMethodDefinition_impl ();
  224. if (res == null)
  225. throw new InvalidOperationException ();
  226. return res;
  227. }
  228. public override extern bool Mono_IsInflatedMethod {
  229. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  230. get;
  231. }
  232. public override extern bool HasGenericParameters {
  233. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  234. get;
  235. }
  236. public override extern bool IsGenericMethodDefinition {
  237. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  238. get;
  239. }
  240. public override MethodBody GetMethodBody () {
  241. return GetMethodBody (mhandle);
  242. }
  243. #endif
  244. }
  245. internal class MonoCMethod : ConstructorInfo, ISerializable
  246. {
  247. internal IntPtr mhandle;
  248. string name;
  249. Type reftype;
  250. public override MethodImplAttributes GetMethodImplementationFlags() {
  251. MonoMethodInfo info;
  252. MonoMethodInfo.get_method_info (mhandle, out info);
  253. return info.iattrs;
  254. }
  255. public override ParameterInfo[] GetParameters() {
  256. return MonoMethodInfo.get_parameter_info (mhandle);
  257. }
  258. /*
  259. * InternalInvoke() receives the parameters corretcly converted by the binder
  260. * to match the types of the method signature.
  261. */
  262. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  263. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  264. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  265. if (binder == null)
  266. binder = Binder.DefaultBinder;
  267. ParameterInfo[] pinfo = GetParameters ();
  268. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  269. throw new ArgumentException ("parameters");
  270. try {
  271. return InternalInvoke (obj, parameters);
  272. } catch (TargetException) {
  273. throw;
  274. } catch (Exception e) {
  275. throw new TargetInvocationException (e);
  276. }
  277. }
  278. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  279. return Invoke (null, invokeAttr, binder, parameters, culture);
  280. }
  281. public override RuntimeMethodHandle MethodHandle {
  282. get {return new RuntimeMethodHandle (mhandle);}
  283. }
  284. public override MethodAttributes Attributes {
  285. get {
  286. MonoMethodInfo info;
  287. MonoMethodInfo.get_method_info (mhandle, out info);
  288. return info.attrs;
  289. }
  290. }
  291. public override CallingConventions CallingConvention {
  292. get {
  293. MonoMethodInfo info;
  294. MonoMethodInfo.get_method_info (mhandle, out info);
  295. return info.callconv;
  296. }
  297. }
  298. public override Type ReflectedType {
  299. get {
  300. return reftype;
  301. }
  302. }
  303. public override Type DeclaringType {
  304. get {
  305. MonoMethodInfo info;
  306. MonoMethodInfo.get_method_info (mhandle, out info);
  307. return info.parent;
  308. }
  309. }
  310. public override string Name {
  311. get {
  312. return name;
  313. }
  314. }
  315. public override bool IsDefined (Type attributeType, bool inherit) {
  316. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  317. }
  318. public override object[] GetCustomAttributes( bool inherit) {
  319. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  320. }
  321. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  322. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  323. }
  324. #if NET_2_0 || BOOTSTRAP_NET_2_0
  325. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  326. extern MethodInfo GetGenericMethodDefinition_impl ();
  327. public override MethodInfo GetGenericMethodDefinition ()
  328. {
  329. MethodInfo res = GetGenericMethodDefinition_impl ();
  330. if (res == null)
  331. throw new InvalidOperationException ();
  332. return res;
  333. }
  334. public override extern bool Mono_IsInflatedMethod {
  335. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  336. get;
  337. }
  338. public override bool HasGenericParameters {
  339. get {
  340. return false;
  341. }
  342. }
  343. public override bool IsGenericMethodDefinition {
  344. get {
  345. return false;
  346. }
  347. }
  348. public override MethodBody GetMethodBody () {
  349. return GetMethodBody (mhandle);
  350. }
  351. #endif
  352. public override string ToString () {
  353. string parms = "";
  354. ParameterInfo[] p = GetParameters ();
  355. for (int i = 0; i < p.Length; ++i) {
  356. if (i > 0)
  357. parms = parms + ", ";
  358. parms = parms + p [i].ParameterType.Name;
  359. }
  360. return "Void "+Name+"("+parms+")";
  361. }
  362. // ISerializable
  363. public void GetObjectData(SerializationInfo info, StreamingContext context)
  364. {
  365. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  366. }
  367. }
  368. }