MonoMethod.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. if (pt.IsByRef)
  199. parms += " ByRef";
  200. }
  201. if (ReturnType.IsClass && ReturnType.Namespace != "")
  202. return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
  203. string generic = "";
  204. #if NET_2_0 || BOOTSTRAP_NET_2_0
  205. if (HasGenericParameters) {
  206. Type[] gen_params = GetGenericArguments ();
  207. generic = "[";
  208. for (int j = 0; j < gen_params.Length; j++) {
  209. if (j > 0)
  210. generic += ",";
  211. generic += gen_params [j].Name;
  212. }
  213. generic += "]";
  214. }
  215. #endif
  216. return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
  217. }
  218. // ISerializable
  219. public void GetObjectData(SerializationInfo info, StreamingContext context)
  220. {
  221. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
  222. }
  223. #if NET_2_0 || BOOTSTRAP_NET_2_0
  224. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  225. public override extern MethodInfo MakeGenericMethod (Type [] types);
  226. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  227. public override extern Type [] GetGenericArguments ();
  228. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  229. extern MethodInfo GetGenericMethodDefinition_impl ();
  230. public override MethodInfo GetGenericMethodDefinition ()
  231. {
  232. MethodInfo res = GetGenericMethodDefinition_impl ();
  233. if (res == null)
  234. throw new InvalidOperationException ();
  235. return res;
  236. }
  237. public override extern bool Mono_IsInflatedMethod {
  238. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  239. get;
  240. }
  241. public override extern bool HasGenericParameters {
  242. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  243. get;
  244. }
  245. public override extern bool IsGenericMethodDefinition {
  246. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  247. get;
  248. }
  249. #endif
  250. #if NET_2_0
  251. public override MethodBody GetMethodBody () {
  252. return GetMethodBody (mhandle);
  253. }
  254. #endif
  255. }
  256. internal class MonoCMethod : ConstructorInfo, ISerializable
  257. {
  258. internal IntPtr mhandle;
  259. string name;
  260. Type reftype;
  261. public override MethodImplAttributes GetMethodImplementationFlags() {
  262. MonoMethodInfo info;
  263. MonoMethodInfo.get_method_info (mhandle, out info);
  264. return info.iattrs;
  265. }
  266. public override ParameterInfo[] GetParameters() {
  267. return MonoMethodInfo.get_parameter_info (mhandle);
  268. }
  269. /*
  270. * InternalInvoke() receives the parameters corretcly converted by the binder
  271. * to match the types of the method signature.
  272. */
  273. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  274. internal extern Object InternalInvoke (Object obj, Object[] parameters);
  275. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  276. {
  277. if (binder == null)
  278. binder = Binder.DefaultBinder;
  279. ParameterInfo[] pinfo = GetParameters ();
  280. if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
  281. throw new ArgumentException ("parameters");
  282. if (SecurityManager.SecurityEnabled) {
  283. // sadly Attributes doesn't tell us which kind of security action this is so
  284. // we must do it the hard way - and it also means that we can skip calling
  285. // Attribute (which is another an icall)
  286. SecurityManager.ReflectedLinkDemandInvoke (this);
  287. }
  288. try {
  289. return InternalInvoke (obj, parameters);
  290. } catch (InvalidOperationException) {
  291. throw;
  292. } catch (TargetException) {
  293. throw;
  294. } catch (Exception e) {
  295. throw new TargetInvocationException (e);
  296. }
  297. }
  298. public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  299. return Invoke (null, invokeAttr, binder, parameters, culture);
  300. }
  301. public override RuntimeMethodHandle MethodHandle {
  302. get {return new RuntimeMethodHandle (mhandle);}
  303. }
  304. public override MethodAttributes Attributes {
  305. get {
  306. MonoMethodInfo info;
  307. MonoMethodInfo.get_method_info (mhandle, out info);
  308. return info.attrs;
  309. }
  310. }
  311. public override CallingConventions CallingConvention {
  312. get {
  313. MonoMethodInfo info;
  314. MonoMethodInfo.get_method_info (mhandle, out info);
  315. return info.callconv;
  316. }
  317. }
  318. public override Type ReflectedType {
  319. get {
  320. return reftype;
  321. }
  322. }
  323. public override Type DeclaringType {
  324. get {
  325. MonoMethodInfo info;
  326. MonoMethodInfo.get_method_info (mhandle, out info);
  327. return info.parent;
  328. }
  329. }
  330. public override string Name {
  331. get {
  332. return name;
  333. }
  334. }
  335. public override bool IsDefined (Type attributeType, bool inherit) {
  336. return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
  337. }
  338. public override object[] GetCustomAttributes( bool inherit) {
  339. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  340. }
  341. public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
  342. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  343. }
  344. #if NET_2_0 || BOOTSTRAP_NET_2_0
  345. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  346. extern MethodInfo GetGenericMethodDefinition_impl ();
  347. public override MethodInfo GetGenericMethodDefinition ()
  348. {
  349. MethodInfo res = GetGenericMethodDefinition_impl ();
  350. if (res == null)
  351. throw new InvalidOperationException ();
  352. return res;
  353. }
  354. public override extern bool Mono_IsInflatedMethod {
  355. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  356. get;
  357. }
  358. public override bool HasGenericParameters {
  359. get {
  360. return false;
  361. }
  362. }
  363. public override bool IsGenericMethodDefinition {
  364. get {
  365. return false;
  366. }
  367. }
  368. #endif
  369. #if NET_2_0
  370. public override MethodBody GetMethodBody () {
  371. return GetMethodBody (mhandle);
  372. }
  373. #endif
  374. public override string ToString () {
  375. string parms = "";
  376. ParameterInfo[] p = GetParameters ();
  377. for (int i = 0; i < p.Length; ++i) {
  378. if (i > 0)
  379. parms = parms + ", ";
  380. parms = parms + p [i].ParameterType.Name;
  381. }
  382. return "Void "+Name+"("+parms+")";
  383. }
  384. // ISerializable
  385. public void GetObjectData(SerializationInfo info, StreamingContext context)
  386. {
  387. ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
  388. }
  389. }
  390. }