MethodBase.cs 9.3 KB

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