MethodBase.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  45. private extern static MethodBase GetMethodFromHandleInternal(IntPtr handle);
  46. public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) {
  47. return GetMethodFromHandleInternal (handle.Value);
  48. }
  49. public abstract MethodImplAttributes GetMethodImplementationFlags();
  50. public abstract ParameterInfo[] GetParameters();
  51. //
  52. // This is a quick version for our own use. We should override
  53. // it where possible so that it does not allocate an array.
  54. //
  55. internal virtual int GetParameterCount ()
  56. {
  57. ParameterInfo [] pi = GetParameters ();
  58. if (pi == null)
  59. return 0;
  60. return pi.Length;
  61. }
  62. [DebuggerHidden]
  63. [DebuggerStepThrough]
  64. #if NET_2_0 || BOOTSTRAP_NET_2_0
  65. virtual
  66. #endif
  67. public Object Invoke(Object obj, Object[] parameters) {
  68. return Invoke (obj, 0, null, parameters, null);
  69. }
  70. public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
  71. protected MethodBase()
  72. {
  73. }
  74. public abstract RuntimeMethodHandle MethodHandle { get; }
  75. public abstract MethodAttributes Attributes { get; }
  76. public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
  77. public Boolean IsPublic {
  78. get {
  79. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  80. }
  81. }
  82. public Boolean IsPrivate {
  83. get {
  84. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  85. }
  86. }
  87. public Boolean IsFamily {
  88. get {
  89. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  90. }
  91. }
  92. public Boolean IsAssembly {
  93. get {
  94. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  95. }
  96. }
  97. public Boolean IsFamilyAndAssembly {
  98. get {
  99. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  100. }
  101. }
  102. public Boolean IsFamilyOrAssembly {
  103. get {
  104. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  105. }
  106. }
  107. public Boolean IsStatic {
  108. get {
  109. return (Attributes & MethodAttributes.Static) != 0;
  110. }
  111. }
  112. public Boolean IsFinal {
  113. get {
  114. return (Attributes & MethodAttributes.Final) != 0;
  115. }
  116. }
  117. public Boolean IsVirtual {
  118. get {
  119. return (Attributes & MethodAttributes.Virtual) != 0;
  120. }
  121. }
  122. public Boolean IsHideBySig {
  123. get {
  124. return (Attributes & MethodAttributes.HideBySig) != 0;
  125. }
  126. }
  127. public Boolean IsAbstract {
  128. get {
  129. return (Attributes & MethodAttributes.Abstract) != 0;
  130. }
  131. }
  132. public Boolean IsSpecialName {
  133. get {
  134. int attr = (int)Attributes;
  135. return (attr & (int)MethodAttributes.SpecialName) != 0;
  136. }
  137. }
  138. public Boolean IsConstructor {
  139. get {
  140. int attr = (int)Attributes;
  141. return ((attr & (int)MethodAttributes.RTSpecialName) != 0
  142. && (Name == ".ctor"));
  143. }
  144. }
  145. internal virtual int get_next_table_index (object obj, int table, bool inc) {
  146. if (this is MethodBuilder) {
  147. MethodBuilder mb = (MethodBuilder)this;
  148. return mb.get_next_table_index (obj, table, inc);
  149. }
  150. if (this is ConstructorBuilder) {
  151. ConstructorBuilder mb = (ConstructorBuilder)this;
  152. return mb.get_next_table_index (obj, table, inc);
  153. }
  154. throw new Exception ("Method is not a builder method");
  155. }
  156. #if NET_2_0 || BOOTSTRAP_NET_2_0
  157. public virtual MethodInfo BindGenericParameters (Type [] types)
  158. {
  159. throw new NotSupportedException ();
  160. }
  161. public virtual Type [] GetGenericArguments ()
  162. {
  163. throw new NotSupportedException ();
  164. }
  165. public virtual MethodInfo GetGenericMethodDefinition ()
  166. {
  167. throw new NotSupportedException ();
  168. }
  169. public virtual bool Mono_IsInflatedMethod {
  170. get {
  171. throw new NotSupportedException ();
  172. }
  173. }
  174. public virtual bool HasGenericParameters {
  175. get {
  176. throw new NotSupportedException ();
  177. }
  178. }
  179. public virtual bool IsGenericMethodDefinition {
  180. get {
  181. throw new NotSupportedException ();
  182. }
  183. }
  184. #endif
  185. #if NET_2_0
  186. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  187. internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
  188. internal static MethodBody GetMethodBody (IntPtr handle) {
  189. MethodBody mb = GetMethodBodyInternal (handle);
  190. if (mb == null)
  191. throw new ArgumentException ("Only methods with IL bodies are supported.");
  192. else
  193. return mb;
  194. }
  195. public virtual MethodBody GetMethodBody () {
  196. throw new NotSupportedException ();
  197. }
  198. #endif
  199. }
  200. }