MethodBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // System.Reflection/MethodBase.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. // Copyright 2011 Xamarin Inc (http://www.xamarin.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.Diagnostics;
  31. using System.Globalization;
  32. #if !FULL_AOT_RUNTIME
  33. using System.Reflection.Emit;
  34. #endif
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection {
  38. [ComVisible (true)]
  39. [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
  40. [Serializable]
  41. [ClassInterface(ClassInterfaceType.None)]
  42. #if MOBILE
  43. public abstract class MethodBase: MemberInfo {
  44. #else
  45. public abstract class MethodBase: MemberInfo, _MethodBase {
  46. #endif
  47. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  48. public extern static MethodBase GetCurrentMethod ();
  49. internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
  50. {
  51. return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
  52. }
  53. static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
  54. {
  55. if (handle == IntPtr.Zero)
  56. throw new ArgumentException ("The handle is invalid.");
  57. MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
  58. if (res == null)
  59. throw new ArgumentException ("The handle is invalid.");
  60. return res;
  61. }
  62. public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
  63. {
  64. MethodBase res = GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
  65. Type t = res.DeclaringType;
  66. if (t.IsGenericType || t.IsGenericTypeDefinition)
  67. throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
  68. return res;
  69. }
  70. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  71. private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
  72. [ComVisible (false)]
  73. public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
  74. {
  75. return GetMethodFromIntPtr (handle.Value, declaringType.Value);
  76. }
  77. public abstract MethodImplAttributes GetMethodImplementationFlags();
  78. public abstract ParameterInfo[] GetParameters();
  79. //
  80. // This is a quick version for our own use. We should override
  81. // it where possible so that it does not allocate an array.
  82. // They cannot be abstract otherwise we break public contract
  83. //
  84. internal virtual ParameterInfo[] GetParametersInternal ()
  85. {
  86. // Override me
  87. return GetParameters ();
  88. }
  89. internal virtual int GetParametersCount ()
  90. {
  91. // Override me
  92. return GetParametersInternal ().Length;
  93. }
  94. internal virtual Type GetParameterType (int pos) {
  95. throw new NotImplementedException ();
  96. }
  97. [DebuggerHidden]
  98. [DebuggerStepThrough]
  99. public Object Invoke(Object obj, Object[] parameters) {
  100. return Invoke (obj, 0, null, parameters, null);
  101. }
  102. public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
  103. protected MethodBase()
  104. {
  105. }
  106. public abstract RuntimeMethodHandle MethodHandle { get; }
  107. public abstract MethodAttributes Attributes { get; }
  108. public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
  109. public Boolean IsPublic {
  110. get {
  111. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  112. }
  113. }
  114. public Boolean IsPrivate {
  115. get {
  116. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  117. }
  118. }
  119. public Boolean IsFamily {
  120. get {
  121. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  122. }
  123. }
  124. public Boolean IsAssembly {
  125. get {
  126. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  127. }
  128. }
  129. public Boolean IsFamilyAndAssembly {
  130. get {
  131. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  132. }
  133. }
  134. public Boolean IsFamilyOrAssembly {
  135. get {
  136. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  137. }
  138. }
  139. public Boolean IsStatic {
  140. get {
  141. return (Attributes & MethodAttributes.Static) != 0;
  142. }
  143. }
  144. public Boolean IsFinal {
  145. get {
  146. return (Attributes & MethodAttributes.Final) != 0;
  147. }
  148. }
  149. public Boolean IsVirtual {
  150. get {
  151. return (Attributes & MethodAttributes.Virtual) != 0;
  152. }
  153. }
  154. public Boolean IsHideBySig {
  155. get {
  156. return (Attributes & MethodAttributes.HideBySig) != 0;
  157. }
  158. }
  159. public Boolean IsAbstract {
  160. get {
  161. return (Attributes & MethodAttributes.Abstract) != 0;
  162. }
  163. }
  164. public Boolean IsSpecialName {
  165. get {
  166. int attr = (int)Attributes;
  167. return (attr & (int)MethodAttributes.SpecialName) != 0;
  168. }
  169. }
  170. [ComVisibleAttribute (true)]
  171. public Boolean IsConstructor {
  172. get {
  173. int attr = (int)Attributes;
  174. return ((attr & (int)MethodAttributes.RTSpecialName) != 0
  175. && (Name == ".ctor"));
  176. }
  177. }
  178. internal virtual int get_next_table_index (object obj, int table, bool inc) {
  179. #if !FULL_AOT_RUNTIME
  180. if (this is MethodBuilder) {
  181. MethodBuilder mb = (MethodBuilder)this;
  182. return mb.get_next_table_index (obj, table, inc);
  183. }
  184. if (this is ConstructorBuilder) {
  185. ConstructorBuilder mb = (ConstructorBuilder)this;
  186. return mb.get_next_table_index (obj, table, inc);
  187. }
  188. #endif
  189. throw new Exception ("Method is not a builder method");
  190. }
  191. [ComVisible (true)]
  192. public virtual Type [] GetGenericArguments ()
  193. {
  194. throw new NotSupportedException ();
  195. }
  196. public virtual bool ContainsGenericParameters {
  197. get {
  198. return false;
  199. }
  200. }
  201. public virtual bool IsGenericMethodDefinition {
  202. get {
  203. return false;
  204. }
  205. }
  206. public virtual bool IsGenericMethod {
  207. get {
  208. return false;
  209. }
  210. }
  211. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  212. internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
  213. internal static MethodBody GetMethodBody (IntPtr handle) {
  214. return GetMethodBodyInternal (handle);
  215. }
  216. public virtual MethodBody GetMethodBody () {
  217. throw new NotSupportedException ();
  218. }
  219. #if NET_4_0
  220. public override bool Equals (object obj)
  221. {
  222. return obj == (object) this;
  223. }
  224. public override int GetHashCode ()
  225. {
  226. return base.GetHashCode ();
  227. }
  228. public static bool operator == (MethodBase left, MethodBase right)
  229. {
  230. if ((object)left == (object)right)
  231. return true;
  232. if ((object)left == null ^ (object)right == null)
  233. return false;
  234. return left.Equals (right);
  235. }
  236. public static bool operator != (MethodBase left, MethodBase right)
  237. {
  238. if ((object)left == (object)right)
  239. return false;
  240. if ((object)left == null ^ (object)right == null)
  241. return true;
  242. return !left.Equals (right);
  243. }
  244. public virtual bool IsSecurityCritical {
  245. get {
  246. throw new NotImplementedException ();
  247. }
  248. }
  249. public virtual bool IsSecuritySafeCritical {
  250. get {
  251. throw new NotImplementedException ();
  252. }
  253. }
  254. public virtual bool IsSecurityTransparent {
  255. get {
  256. throw new NotImplementedException ();
  257. }
  258. }
  259. #endif
  260. #if !MOBILE
  261. void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  262. {
  263. throw new NotImplementedException ();
  264. }
  265. Type _MethodBase.GetType ()
  266. {
  267. // Required or object::GetType becomes virtual final
  268. return base.GetType ();
  269. }
  270. void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  271. {
  272. throw new NotImplementedException ();
  273. }
  274. void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
  275. {
  276. throw new NotImplementedException ();
  277. }
  278. void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  279. {
  280. throw new NotImplementedException ();
  281. }
  282. #endif
  283. }
  284. }