MonoMethod.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Globalization;
  31. using System.Runtime.CompilerServices;
  32. using System.Runtime.InteropServices;
  33. using System.Runtime.Serialization;
  34. using System.Reflection.Emit;
  35. using System.Security;
  36. using System.Threading;
  37. using System.Text;
  38. namespace System.Reflection {
  39. internal struct MonoMethodInfo
  40. {
  41. internal Type parent;
  42. internal Type ret;
  43. internal MethodAttributes attrs;
  44. internal MethodImplAttributes iattrs;
  45. internal CallingConventions callconv;
  46. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  47. internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
  48. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  49. internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
  50. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  51. internal static extern UnmanagedMarshal get_retval_marshal (IntPtr handle);
  52. };
  53. /*
  54. * Note: most of this class needs to be duplicated for the contructor, since
  55. * the .NET reflection class hierarchy is so broken.
  56. */
  57. [Serializable()]
  58. internal class MonoMethod : MethodInfo, ISerializable
  59. {
  60. internal IntPtr mhandle;
  61. string name;
  62. Type reftype;
  63. internal MonoMethod () {
  64. }
  65. internal MonoMethod (RuntimeMethodHandle mhandle) {
  66. this.mhandle = mhandle.Value;
  67. }
  68. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  69. internal static extern string get_name (MethodBase method);
  70. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  71. internal static extern MonoMethod get_base_definition (MonoMethod method);
  72. public override MethodInfo GetBaseDefinition ()
  73. {
  74. return get_base_definition (this);
  75. }
  76. #if NET_2_0 || BOOTSTRAP_NET_2_0
  77. public override ParameterInfo ReturnParameter {
  78. get {
  79. return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
  80. }
  81. }
  82. #endif
  83. public override Type ReturnType {
  84. get {
  85. MonoMethodInfo info;
  86. MonoMethodInfo.get_method_info (mhandle, out info);
  87. return info.ret;
  88. }
  89. }
  90. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  91. get {
  92. return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
  93. }
  94. }
  95. public override MethodImplAttributes GetMethodImplementationFlags() {
  96. MonoMethodInfo info;
  97. MonoMethodInfo.get_method_info (mhandle, out info);
  98. return info.iattrs;
  99. }
  100. public override ParameterInfo[] GetParameters() {
  101. return MonoMethodInfo.get_parameter_info (mhandle);
  102. }
  103. /*
  104. * InternalInvoke() receives the parameters correctly converted by the
  105. * binder to match the types of the method signature.
  106. */
  107. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  108. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  109. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  110. {
  111. if (binder == null)
  112. binder = Binder.DefaultBinder;
  113. ParameterInfo[] pinfo = GetParameters ();
  114. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  115. throw new ArgumentException ("parameters");
  116. if (SecurityManager.SecurityEnabled) {
  117. // sadly Attributes doesn't tell us which kind of security action this is so
  118. // we must do it the hard way - and it also means that we can skip calling
  119. // Attribute (which is another an icall)
  120. SecurityManager.ReflectedLinkDemandInvoke (this);
  121. }
  122. #if NET_2_0
  123. if (ContainsGenericParameters)
  124. throw new InvalidOperationException ("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.");
  125. #endif
  126. try {
  127. return InternalInvoke (obj, parameters);
  128. } catch (InvalidOperationException) {
  129. throw;
  130. } catch (TargetException) {
  131. throw;
  132. #if NET_2_0
  133. } catch (ThreadAbortException) {
  134. throw;
  135. #endif
  136. #if NET_2_1
  137. } catch (MethodAccessException) {
  138. throw;
  139. #endif
  140. } catch (Exception e) {
  141. throw new TargetInvocationException (e);
  142. }
  143. }
  144. public override RuntimeMethodHandle MethodHandle {
  145. get {return new RuntimeMethodHandle (mhandle);}
  146. }
  147. public override MethodAttributes Attributes {
  148. get {
  149. MonoMethodInfo info;
  150. MonoMethodInfo.get_method_info (mhandle, out info);
  151. return info.attrs;
  152. }
  153. }
  154. public override CallingConventions CallingConvention {
  155. get {
  156. MonoMethodInfo info;
  157. MonoMethodInfo.get_method_info (mhandle, out info);
  158. return info.callconv;
  159. }
  160. }
  161. public override Type ReflectedType {
  162. get {
  163. return reftype;
  164. }
  165. }
  166. public override Type DeclaringType {
  167. get {
  168. MonoMethodInfo info;
  169. MonoMethodInfo.get_method_info (mhandle, out info);
  170. return info.parent;
  171. }
  172. }
  173. public override string Name {
  174. get {
  175. if (name != null)
  176. return name;
  177. return get_name (this);
  178. }
  179. }
  180. public override bool IsDefined (Type attributeType, bool inherit) {
  181. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  182. }
  183. public override object[] GetCustomAttributes( bool inherit) {
  184. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  185. }
  186. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  187. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  188. }
  189. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  190. internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
  191. internal object[] GetPseudoCustomAttributes ()
  192. {
  193. int count = 0;
  194. /* MS.NET doesn't report MethodImplAttribute */
  195. MonoMethodInfo info;
  196. MonoMethodInfo.get_method_info (mhandle, out info);
  197. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  198. count ++;
  199. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
  200. count ++;
  201. if (count == 0)
  202. return null;
  203. object[] attrs = new object [count];
  204. count = 0;
  205. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  206. attrs [count ++] = new PreserveSigAttribute ();
  207. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
  208. DllImportAttribute attr = GetDllImportAttribute (mhandle);
  209. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  210. attr.PreserveSig = true;
  211. attrs [count ++] = attr;
  212. }
  213. return attrs;
  214. }
  215. public override string ToString () {
  216. StringBuilder sb = new StringBuilder ();
  217. if (ReturnType.IsClass)
  218. sb.Append (ReturnType.ToString ());
  219. else
  220. sb.Append (ReturnType.Name);
  221. sb.Append (" ");
  222. sb.Append (Name);
  223. #if NET_2_0 || BOOTSTRAP_NET_2_0
  224. if (IsGenericMethod) {
  225. Type[] gen_params = GetGenericArguments ();
  226. sb.Append ("[");
  227. for (int j = 0; j < gen_params.Length; j++) {
  228. if (j > 0)
  229. sb.Append (",");
  230. sb.Append (gen_params [j].Name);
  231. }
  232. sb.Append ("]");
  233. }
  234. #endif
  235. sb.Append ("(");
  236. ParameterInfo[] p = GetParameters ();
  237. for (int i = 0; i < p.Length; ++i) {
  238. if (i > 0)
  239. sb.Append (", ");
  240. Type pt = p[i].ParameterType;
  241. bool byref = pt.IsByRef;
  242. if (byref)
  243. pt = pt.GetElementType ();
  244. if (pt.IsClass)
  245. sb.Append (pt.ToString ());
  246. else
  247. sb.Append (pt.Name);
  248. if (byref)
  249. sb.Append (" ByRef");
  250. }
  251. sb.Append (")");
  252. return sb.ToString ();
  253. }
  254. // ISerializable
  255. public void GetObjectData(SerializationInfo info, StreamingContext context)
  256. {
  257. #if NET_2_0
  258. Type[] genericArguments = IsGenericMethod && !IsGenericMethodDefinition
  259. ? GetGenericArguments () : null;
  260. MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method, genericArguments);
  261. #else
  262. MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  263. #endif
  264. }
  265. #if NET_2_0 || BOOTSTRAP_NET_2_0
  266. public override MethodInfo MakeGenericMethod (Type [] methodInstantiation)
  267. {
  268. if (methodInstantiation == null)
  269. throw new ArgumentNullException ("methodInstantiation");
  270. foreach (Type type in methodInstantiation)
  271. if (type == null)
  272. throw new ArgumentNullException ();
  273. MethodInfo ret = MakeGenericMethod_impl (methodInstantiation);
  274. if (ret == null)
  275. throw new ArgumentException (String.Format ("The method has {0} generic parameter(s) but {1} generic argument(s) were provided.", GetGenericArguments ().Length, methodInstantiation.Length));
  276. return ret;
  277. }
  278. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  279. extern MethodInfo MakeGenericMethod_impl (Type [] types);
  280. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  281. public override extern Type [] GetGenericArguments ();
  282. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  283. extern MethodInfo GetGenericMethodDefinition_impl ();
  284. public override MethodInfo GetGenericMethodDefinition ()
  285. {
  286. MethodInfo res = GetGenericMethodDefinition_impl ();
  287. if (res == null)
  288. throw new InvalidOperationException ();
  289. return res;
  290. }
  291. public override extern bool IsGenericMethodDefinition {
  292. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  293. get;
  294. }
  295. public override extern bool IsGenericMethod {
  296. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  297. get;
  298. }
  299. public override bool ContainsGenericParameters {
  300. get {
  301. if (IsGenericMethod) {
  302. foreach (Type arg in GetGenericArguments ())
  303. if (arg.ContainsGenericParameters)
  304. return true;
  305. }
  306. return DeclaringType.ContainsGenericParameters;
  307. }
  308. }
  309. #endif
  310. #if NET_2_0
  311. public override MethodBody GetMethodBody () {
  312. return GetMethodBody (mhandle);
  313. }
  314. #endif
  315. }
  316. internal class MonoCMethod : ConstructorInfo, ISerializable
  317. {
  318. internal IntPtr mhandle;
  319. string name;
  320. Type reftype;
  321. public override MethodImplAttributes GetMethodImplementationFlags() {
  322. MonoMethodInfo info;
  323. MonoMethodInfo.get_method_info (mhandle, out info);
  324. return info.iattrs;
  325. }
  326. public override ParameterInfo[] GetParameters() {
  327. return MonoMethodInfo.get_parameter_info (mhandle);
  328. }
  329. /*
  330. * InternalInvoke() receives the parameters corretcly converted by the binder
  331. * to match the types of the method signature.
  332. */
  333. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  334. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  335. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  336. {
  337. if (binder == null)
  338. binder = Binder.DefaultBinder;
  339. ParameterInfo[] pinfo = GetParameters ();
  340. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  341. throw new ArgumentException ("parameters");
  342. if (SecurityManager.SecurityEnabled) {
  343. // sadly Attributes doesn't tell us which kind of security action this is so
  344. // we must do it the hard way - and it also means that we can skip calling
  345. // Attribute (which is another an icall)
  346. SecurityManager.ReflectedLinkDemandInvoke (this);
  347. }
  348. try {
  349. return InternalInvoke (obj, parameters);
  350. } catch (InvalidOperationException) {
  351. throw;
  352. } catch (TargetException) {
  353. throw;
  354. #if NET_2_1
  355. } catch (MethodAccessException) {
  356. throw;
  357. #endif
  358. } catch (Exception e) {
  359. throw new TargetInvocationException (e);
  360. }
  361. }
  362. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  363. return Invoke (null, invokeAttr, binder, parameters, culture);
  364. }
  365. public override RuntimeMethodHandle MethodHandle {
  366. get {return new RuntimeMethodHandle (mhandle);}
  367. }
  368. public override MethodAttributes Attributes {
  369. get {
  370. MonoMethodInfo info;
  371. MonoMethodInfo.get_method_info (mhandle, out info);
  372. return info.attrs;
  373. }
  374. }
  375. public override CallingConventions CallingConvention {
  376. get {
  377. MonoMethodInfo info;
  378. MonoMethodInfo.get_method_info (mhandle, out info);
  379. return info.callconv;
  380. }
  381. }
  382. public override Type ReflectedType {
  383. get {
  384. return reftype;
  385. }
  386. }
  387. public override Type DeclaringType {
  388. get {
  389. MonoMethodInfo info;
  390. MonoMethodInfo.get_method_info (mhandle, out info);
  391. return info.parent;
  392. }
  393. }
  394. public override string Name {
  395. get {
  396. if (name != null)
  397. return name;
  398. return MonoMethod.get_name (this);
  399. }
  400. }
  401. public override bool IsDefined (Type attributeType, bool inherit) {
  402. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  403. }
  404. public override object[] GetCustomAttributes( bool inherit) {
  405. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  406. }
  407. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  408. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  409. }
  410. #if NET_2_0
  411. public override MethodBody GetMethodBody () {
  412. return GetMethodBody (mhandle);
  413. }
  414. #endif
  415. public override string ToString () {
  416. StringBuilder sb = new StringBuilder ();
  417. sb.Append ("Void ");
  418. sb.Append (Name);
  419. sb.Append ("(");
  420. ParameterInfo[] p = GetParameters ();
  421. for (int i = 0; i < p.Length; ++i) {
  422. if (i > 0)
  423. sb.Append (", ");
  424. sb.Append (p[i].ParameterType.Name);
  425. }
  426. sb.Append (")");
  427. return sb.ToString ();
  428. }
  429. // ISerializable
  430. public void GetObjectData(SerializationInfo info, StreamingContext context)
  431. {
  432. MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  433. }
  434. }
  435. }