MethodBase.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Diagnostics;
  30. using System.Globalization;
  31. using System.Reflection.Emit;
  32. using System.Runtime.CompilerServices;
  33. using System.Runtime.InteropServices;
  34. namespace System.Reflection {
  35. [ComVisible (true)]
  36. [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
  37. [Serializable]
  38. [ClassInterface(ClassInterfaceType.None)]
  39. public abstract class MethodBase: MemberInfo, _MethodBase {
  40. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  41. public extern static MethodBase GetCurrentMethod ();
  42. internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
  43. {
  44. return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
  45. }
  46. static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
  47. {
  48. if (handle == IntPtr.Zero)
  49. throw new ArgumentException ("The handle is invalid.");
  50. MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
  51. if (res == null)
  52. throw new ArgumentException ("The handle is invalid.");
  53. return res;
  54. }
  55. public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
  56. {
  57. MethodBase res = GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
  58. Type t = res.DeclaringType;
  59. if (t.IsGenericType || t.IsGenericTypeDefinition)
  60. throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
  61. return res;
  62. }
  63. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  64. private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
  65. [ComVisible (false)]
  66. public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
  67. {
  68. return GetMethodFromIntPtr (handle.Value, declaringType.Value);
  69. }
  70. public abstract MethodImplAttributes GetMethodImplementationFlags();
  71. public abstract ParameterInfo[] GetParameters();
  72. //
  73. // This is a quick version for our own use. We should override
  74. // it where possible so that it does not allocate an array.
  75. //
  76. internal abstract int GetParameterCount ();
  77. [DebuggerHidden]
  78. [DebuggerStepThrough]
  79. public Object Invoke(Object obj, Object[] parameters) {
  80. return Invoke (obj, 0, null, parameters, null);
  81. }
  82. public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
  83. protected MethodBase()
  84. {
  85. }
  86. public abstract RuntimeMethodHandle MethodHandle { get; }
  87. public abstract MethodAttributes Attributes { get; }
  88. public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
  89. public Boolean IsPublic {
  90. get {
  91. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  92. }
  93. }
  94. public Boolean IsPrivate {
  95. get {
  96. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  97. }
  98. }
  99. public Boolean IsFamily {
  100. get {
  101. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  102. }
  103. }
  104. public Boolean IsAssembly {
  105. get {
  106. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  107. }
  108. }
  109. public Boolean IsFamilyAndAssembly {
  110. get {
  111. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  112. }
  113. }
  114. public Boolean IsFamilyOrAssembly {
  115. get {
  116. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  117. }
  118. }
  119. public Boolean IsStatic {
  120. get {
  121. return (Attributes & MethodAttributes.Static) != 0;
  122. }
  123. }
  124. public Boolean IsFinal {
  125. get {
  126. return (Attributes & MethodAttributes.Final) != 0;
  127. }
  128. }
  129. public Boolean IsVirtual {
  130. get {
  131. return (Attributes & MethodAttributes.Virtual) != 0;
  132. }
  133. }
  134. public Boolean IsHideBySig {
  135. get {
  136. return (Attributes & MethodAttributes.HideBySig) != 0;
  137. }
  138. }
  139. public Boolean IsAbstract {
  140. get {
  141. return (Attributes & MethodAttributes.Abstract) != 0;
  142. }
  143. }
  144. public Boolean IsSpecialName {
  145. get {
  146. int attr = (int)Attributes;
  147. return (attr & (int)MethodAttributes.SpecialName) != 0;
  148. }
  149. }
  150. [ComVisibleAttribute (true)]
  151. public Boolean IsConstructor {
  152. get {
  153. int attr = (int)Attributes;
  154. return ((attr & (int)MethodAttributes.RTSpecialName) != 0
  155. && (Name == ".ctor"));
  156. }
  157. }
  158. internal virtual int get_next_table_index (object obj, int table, bool inc) {
  159. if (this is MethodBuilder) {
  160. MethodBuilder mb = (MethodBuilder)this;
  161. return mb.get_next_table_index (obj, table, inc);
  162. }
  163. if (this is ConstructorBuilder) {
  164. ConstructorBuilder mb = (ConstructorBuilder)this;
  165. return mb.get_next_table_index (obj, table, inc);
  166. }
  167. throw new Exception ("Method is not a builder method");
  168. }
  169. [ComVisible (true)]
  170. public virtual Type [] GetGenericArguments ()
  171. {
  172. throw new NotSupportedException ();
  173. }
  174. public virtual bool ContainsGenericParameters {
  175. get {
  176. return false;
  177. }
  178. }
  179. public virtual bool IsGenericMethodDefinition {
  180. get {
  181. return false;
  182. }
  183. }
  184. public virtual bool IsGenericMethod {
  185. get {
  186. return false;
  187. }
  188. }
  189. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  190. internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
  191. internal static MethodBody GetMethodBody (IntPtr handle) {
  192. return GetMethodBodyInternal (handle);
  193. }
  194. public virtual MethodBody GetMethodBody () {
  195. throw new NotSupportedException ();
  196. }
  197. #if NET_4_0
  198. public override bool Equals (object obj)
  199. {
  200. return obj == this;
  201. }
  202. public override int GetHashCode ()
  203. {
  204. return base.GetHashCode ();
  205. }
  206. public static bool operator == (MethodBase left, MethodBase right)
  207. {
  208. if ((object)left == (object)right)
  209. return true;
  210. if ((object)left == null ^ (object)right == null)
  211. return false;
  212. return left.Equals (right);
  213. }
  214. public static bool operator != (MethodBase left, MethodBase right)
  215. {
  216. if ((object)left == (object)right)
  217. return false;
  218. if ((object)left == null ^ (object)right == null)
  219. return true;
  220. return !left.Equals (right);
  221. }
  222. #endif
  223. void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  224. {
  225. throw new NotImplementedException ();
  226. }
  227. void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  228. {
  229. throw new NotImplementedException ();
  230. }
  231. void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
  232. {
  233. throw new NotImplementedException ();
  234. }
  235. void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  236. {
  237. throw new NotImplementedException ();
  238. }
  239. }
  240. }