MethodBase.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #if NET_2_0
  36. [ComVisible (true)]
  37. [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
  38. #endif
  39. [Serializable]
  40. [ClassInterface(ClassInterfaceType.None)]
  41. public abstract class MethodBase: MemberInfo, _MethodBase {
  42. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  43. public extern static MethodBase GetCurrentMethod ();
  44. internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle) {
  45. if (handle.Value == IntPtr.Zero)
  46. throw new ArgumentException ("The handle is invalid.");
  47. MethodBase res = GetMethodFromHandleInternalType (handle.Value, IntPtr.Zero);
  48. if (res == null)
  49. throw new ArgumentException ("The handle is invalid.");
  50. return res;
  51. }
  52. public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) {
  53. MethodBase res = GetMethodFromHandleNoGenericCheck (handle);
  54. #if NET_2_0
  55. Type t = res.DeclaringType;
  56. if (t.IsGenericType || t.IsGenericTypeDefinition)
  57. throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
  58. #endif
  59. return res;
  60. }
  61. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  62. private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
  63. #if NET_2_0
  64. [ComVisible (false)]
  65. public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType) {
  66. if (handle.Value == IntPtr.Zero)
  67. throw new ArgumentException ("The handle is invalid.");
  68. MethodBase res = GetMethodFromHandleInternalType (handle.Value, declaringType.Value);
  69. if (res == null)
  70. throw new ArgumentException ("The handle is invalid.");
  71. return res;
  72. }
  73. #endif
  74. public abstract MethodImplAttributes GetMethodImplementationFlags();
  75. public abstract ParameterInfo[] GetParameters();
  76. //
  77. // This is a quick version for our own use. We should override
  78. // it where possible so that it does not allocate an array.
  79. //
  80. internal virtual int GetParameterCount ()
  81. {
  82. ParameterInfo [] pi = GetParameters ();
  83. if (pi == null)
  84. return 0;
  85. return pi.Length;
  86. }
  87. #if ONLY_1_1
  88. public new Type GetType ()
  89. {
  90. return base.GetType ();
  91. }
  92. #endif
  93. [DebuggerHidden]
  94. [DebuggerStepThrough]
  95. public Object Invoke(Object obj, Object[] parameters) {
  96. return Invoke (obj, 0, null, parameters, null);
  97. }
  98. public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
  99. protected MethodBase()
  100. {
  101. }
  102. public abstract RuntimeMethodHandle MethodHandle { get; }
  103. public abstract MethodAttributes Attributes { get; }
  104. public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
  105. public Boolean IsPublic {
  106. get {
  107. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  108. }
  109. }
  110. public Boolean IsPrivate {
  111. get {
  112. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  113. }
  114. }
  115. public Boolean IsFamily {
  116. get {
  117. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  118. }
  119. }
  120. public Boolean IsAssembly {
  121. get {
  122. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  123. }
  124. }
  125. public Boolean IsFamilyAndAssembly {
  126. get {
  127. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  128. }
  129. }
  130. public Boolean IsFamilyOrAssembly {
  131. get {
  132. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  133. }
  134. }
  135. public Boolean IsStatic {
  136. get {
  137. return (Attributes & MethodAttributes.Static) != 0;
  138. }
  139. }
  140. public Boolean IsFinal {
  141. get {
  142. return (Attributes & MethodAttributes.Final) != 0;
  143. }
  144. }
  145. public Boolean IsVirtual {
  146. get {
  147. return (Attributes & MethodAttributes.Virtual) != 0;
  148. }
  149. }
  150. public Boolean IsHideBySig {
  151. get {
  152. return (Attributes & MethodAttributes.HideBySig) != 0;
  153. }
  154. }
  155. public Boolean IsAbstract {
  156. get {
  157. return (Attributes & MethodAttributes.Abstract) != 0;
  158. }
  159. }
  160. public Boolean IsSpecialName {
  161. get {
  162. int attr = (int)Attributes;
  163. return (attr & (int)MethodAttributes.SpecialName) != 0;
  164. }
  165. }
  166. [ComVisibleAttribute (true)]
  167. public Boolean IsConstructor {
  168. get {
  169. int attr = (int)Attributes;
  170. return ((attr & (int)MethodAttributes.RTSpecialName) != 0
  171. && (Name == ".ctor"));
  172. }
  173. }
  174. internal virtual int get_next_table_index (object obj, int table, bool inc) {
  175. if (this is MethodBuilder) {
  176. MethodBuilder mb = (MethodBuilder)this;
  177. return mb.get_next_table_index (obj, table, inc);
  178. }
  179. if (this is ConstructorBuilder) {
  180. ConstructorBuilder mb = (ConstructorBuilder)this;
  181. return mb.get_next_table_index (obj, table, inc);
  182. }
  183. throw new Exception ("Method is not a builder method");
  184. }
  185. #if NET_2_0 || BOOTSTRAP_NET_2_0
  186. [ComVisible (true)]
  187. public virtual Type [] GetGenericArguments ()
  188. {
  189. throw new NotSupportedException ();
  190. }
  191. public virtual bool ContainsGenericParameters {
  192. get {
  193. return false;
  194. }
  195. }
  196. public virtual bool IsGenericMethodDefinition {
  197. get {
  198. return false;
  199. }
  200. }
  201. public virtual bool IsGenericMethod {
  202. get {
  203. return false;
  204. }
  205. }
  206. #endif
  207. #if NET_2_0
  208. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  209. internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
  210. internal static MethodBody GetMethodBody (IntPtr handle) {
  211. return GetMethodBodyInternal (handle);
  212. }
  213. public virtual MethodBody GetMethodBody () {
  214. throw new NotSupportedException ();
  215. }
  216. #endif
  217. void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  218. {
  219. throw new NotImplementedException ();
  220. }
  221. void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  230. {
  231. throw new NotImplementedException ();
  232. }
  233. }
  234. }