2
0

MonoMethod.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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.Security;
  35. namespace System.Reflection {
  36. internal struct MonoMethodInfo
  37. {
  38. internal Type parent;
  39. internal Type ret;
  40. internal MethodAttributes attrs;
  41. internal MethodImplAttributes iattrs;
  42. internal CallingConventions callconv;
  43. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  44. internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
  45. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  46. internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
  47. };
  48. /*
  49. * Note: most of this class needs to be duplicated for the contructor, since
  50. * the .NET reflection class hierarchy is so broken.
  51. */
  52. [Serializable()]
  53. internal class MonoMethod : MethodInfo, ISerializable
  54. {
  55. internal IntPtr mhandle;
  56. string name;
  57. Type reftype;
  58. internal MonoMethod () {
  59. }
  60. internal MonoMethod (RuntimeMethodHandle mhandle) {
  61. this.mhandle = mhandle.Value;
  62. }
  63. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  64. internal static extern MonoMethod get_base_definition (MonoMethod method);
  65. public override MethodInfo GetBaseDefinition ()
  66. {
  67. return get_base_definition (this);
  68. }
  69. public override Type ReturnType {
  70. get {
  71. MonoMethodInfo info;
  72. MonoMethodInfo.get_method_info (mhandle, out info);
  73. return info.ret;
  74. }
  75. }
  76. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  77. get {
  78. return new ParameterInfo (ReturnType, this);
  79. }
  80. }
  81. public override MethodImplAttributes GetMethodImplementationFlags() {
  82. MonoMethodInfo info;
  83. MonoMethodInfo.get_method_info (mhandle, out info);
  84. return info.iattrs;
  85. }
  86. public override ParameterInfo[] GetParameters() {
  87. return MonoMethodInfo.get_parameter_info (mhandle);
  88. }
  89. /*
  90. * InternalInvoke() receives the parameters correctly converted by the
  91. * binder to match the types of the method signature.
  92. */
  93. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  94. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  95. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  96. {
  97. if (binder == null)
  98. binder = Binder.DefaultBinder;
  99. ParameterInfo[] pinfo = GetParameters ();
  100. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  101. throw new ArgumentException ("parameters");
  102. if (SecurityManager.SecurityEnabled) {
  103. // sadly Attributes doesn't tell us which kind of security action this is so
  104. // we must do it the hard way - and it also means that we can skip calling
  105. // Attribute (which is another an icall)
  106. SecurityManager.ReflectedLinkDemandInvoke (this);
  107. }
  108. try {
  109. return InternalInvoke (obj, parameters);
  110. } catch (InvalidOperationException) {
  111. throw;
  112. } catch (TargetException) {
  113. throw;
  114. } catch (Exception e) {
  115. throw new TargetInvocationException (e);
  116. }
  117. }
  118. public override RuntimeMethodHandle MethodHandle {
  119. get {return new RuntimeMethodHandle (mhandle);}
  120. }
  121. public override MethodAttributes Attributes {
  122. get {
  123. MonoMethodInfo info;
  124. MonoMethodInfo.get_method_info (mhandle, out info);
  125. return info.attrs;
  126. }
  127. }
  128. public override CallingConventions CallingConvention {
  129. get {
  130. MonoMethodInfo info;
  131. MonoMethodInfo.get_method_info (mhandle, out info);
  132. return info.callconv;
  133. }
  134. }
  135. public override Type ReflectedType {
  136. get {
  137. return reftype;
  138. }
  139. }
  140. public override Type DeclaringType {
  141. get {
  142. MonoMethodInfo info;
  143. MonoMethodInfo.get_method_info (mhandle, out info);
  144. return info.parent;
  145. }
  146. }
  147. public override string Name {
  148. get {
  149. return name;
  150. }
  151. }
  152. public override bool IsDefined (Type attributeType, bool inherit) {
  153. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  154. }
  155. public override object[] GetCustomAttributes( bool inherit) {
  156. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  157. }
  158. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  159. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  160. }
  161. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  162. internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
  163. internal object[] GetPseudoCustomAttributes ()
  164. {
  165. int count = 0;
  166. /* MS.NET doesn't report MethodImplAttribute */
  167. MonoMethodInfo info;
  168. MonoMethodInfo.get_method_info (mhandle, out info);
  169. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  170. count ++;
  171. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
  172. count ++;
  173. if (count == 0)
  174. return null;
  175. object[] attrs = new object [count];
  176. count = 0;
  177. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  178. attrs [count ++] = new PreserveSigAttribute ();
  179. if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
  180. DllImportAttribute attr = GetDllImportAttribute (mhandle);
  181. if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
  182. attr.PreserveSig = true;
  183. attrs [count ++] = attr;
  184. }
  185. return attrs;
  186. }
  187. public override string ToString () {
  188. string parms = "";
  189. ParameterInfo[] p = GetParameters ();
  190. for (int i = 0; i < p.Length; ++i) {
  191. if (i > 0)
  192. parms = parms + ", ";
  193. Type pt = p[i].ParameterType;
  194. if (pt.IsClass && pt.Namespace != "")
  195. parms = parms + pt.Namespace + "." + pt.Name;
  196. else
  197. parms = parms + pt.Name;
  198. }
  199. if (ReturnType.IsClass && ReturnType.Namespace != "")
  200. return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
  201. string generic = "";
  202. #if NET_2_0 || BOOTSTRAP_NET_2_0
  203. if (HasGenericParameters) {
  204. Type[] gen_params = GetGenericArguments ();
  205. generic = "[";
  206. for (int j = 0; j < gen_params.Length; j++) {
  207. if (j > 0)
  208. generic += ",";
  209. generic += gen_params [j].Name;
  210. }
  211. generic += "]";
  212. }
  213. #endif
  214. return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
  215. }
  216. // ISerializable
  217. public void GetObjectData(SerializationInfo info, StreamingContext context)
  218. {
  219. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  220. }
  221. #if NET_2_0 || BOOTSTRAP_NET_2_0
  222. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  223. public override extern MethodInfo MakeGenericMethod (Type [] types);
  224. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  225. public override extern Type [] GetGenericArguments ();
  226. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  227. extern MethodInfo GetGenericMethodDefinition_impl ();
  228. public override MethodInfo GetGenericMethodDefinition ()
  229. {
  230. MethodInfo res = GetGenericMethodDefinition_impl ();
  231. if (res == null)
  232. throw new InvalidOperationException ();
  233. return res;
  234. }
  235. public override extern bool Mono_IsInflatedMethod {
  236. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  237. get;
  238. }
  239. public override extern bool HasGenericParameters {
  240. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  241. get;
  242. }
  243. public override extern bool IsGenericMethodDefinition {
  244. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  245. get;
  246. }
  247. #endif
  248. #if NET_2_0
  249. public override MethodBody GetMethodBody () {
  250. return GetMethodBody (mhandle);
  251. }
  252. #endif
  253. }
  254. internal class MonoCMethod : ConstructorInfo, ISerializable
  255. {
  256. internal IntPtr mhandle;
  257. string name;
  258. Type reftype;
  259. public override MethodImplAttributes GetMethodImplementationFlags() {
  260. MonoMethodInfo info;
  261. MonoMethodInfo.get_method_info (mhandle, out info);
  262. return info.iattrs;
  263. }
  264. public override ParameterInfo[] GetParameters() {
  265. return MonoMethodInfo.get_parameter_info (mhandle);
  266. }
  267. /*
  268. * InternalInvoke() receives the parameters corretcly converted by the binder
  269. * to match the types of the method signature.
  270. */
  271. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  272. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  273. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  274. {
  275. if (binder == null)
  276. binder = Binder.DefaultBinder;
  277. ParameterInfo[] pinfo = GetParameters ();
  278. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  279. throw new ArgumentException ("parameters");
  280. if (SecurityManager.SecurityEnabled) {
  281. // sadly Attributes doesn't tell us which kind of security action this is so
  282. // we must do it the hard way - and it also means that we can skip calling
  283. // Attribute (which is another an icall)
  284. SecurityManager.ReflectedLinkDemandInvoke (this);
  285. }
  286. try {
  287. return InternalInvoke (obj, parameters);
  288. } catch (InvalidOperationException) {
  289. throw;
  290. } catch (TargetException) {
  291. throw;
  292. } catch (Exception e) {
  293. throw new TargetInvocationException (e);
  294. }
  295. }
  296. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  297. return Invoke (null, invokeAttr, binder, parameters, culture);
  298. }
  299. public override RuntimeMethodHandle MethodHandle {
  300. get {return new RuntimeMethodHandle (mhandle);}
  301. }
  302. public override MethodAttributes Attributes {
  303. get {
  304. MonoMethodInfo info;
  305. MonoMethodInfo.get_method_info (mhandle, out info);
  306. return info.attrs;
  307. }
  308. }
  309. public override CallingConventions CallingConvention {
  310. get {
  311. MonoMethodInfo info;
  312. MonoMethodInfo.get_method_info (mhandle, out info);
  313. return info.callconv;
  314. }
  315. }
  316. public override Type ReflectedType {
  317. get {
  318. return reftype;
  319. }
  320. }
  321. public override Type DeclaringType {
  322. get {
  323. MonoMethodInfo info;
  324. MonoMethodInfo.get_method_info (mhandle, out info);
  325. return info.parent;
  326. }
  327. }
  328. public override string Name {
  329. get {
  330. return name;
  331. }
  332. }
  333. public override bool IsDefined (Type attributeType, bool inherit) {
  334. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  335. }
  336. public override object[] GetCustomAttributes( bool inherit) {
  337. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  338. }
  339. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  340. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  341. }
  342. #if NET_2_0 || BOOTSTRAP_NET_2_0
  343. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  344. extern MethodInfo GetGenericMethodDefinition_impl ();
  345. public override MethodInfo GetGenericMethodDefinition ()
  346. {
  347. MethodInfo res = GetGenericMethodDefinition_impl ();
  348. if (res == null)
  349. throw new InvalidOperationException ();
  350. return res;
  351. }
  352. public override extern bool Mono_IsInflatedMethod {
  353. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  354. get;
  355. }
  356. public override bool HasGenericParameters {
  357. get {
  358. return false;
  359. }
  360. }
  361. public override bool IsGenericMethodDefinition {
  362. get {
  363. return false;
  364. }
  365. }
  366. #endif
  367. #if NET_2_0
  368. public override MethodBody GetMethodBody () {
  369. return GetMethodBody (mhandle);
  370. }
  371. #endif
  372. public override string ToString () {
  373. string parms = "";
  374. ParameterInfo[] p = GetParameters ();
  375. for (int i = 0; i < p.Length; ++i) {
  376. if (i > 0)
  377. parms = parms + ", ";
  378. parms = parms + p [i].ParameterType.Name;
  379. }
  380. return "Void "+Name+"("+parms+")";
  381. }
  382. // ISerializable
  383. public void GetObjectData(SerializationInfo info, StreamingContext context)
  384. {
  385. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  386. }
  387. }
  388. }