MethodBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Diagnostics;
  33. using System.Globalization;
  34. using System.Reflection.Emit;
  35. using System.Runtime.CompilerServices;
  36. using System.Runtime.InteropServices;
  37. namespace System.Reflection {
  38. [Serializable]
  39. [ClassInterface(ClassInterfaceType.AutoDual)]
  40. public abstract class MethodBase: MemberInfo {
  41. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  42. public extern static MethodBase GetCurrentMethod ();
  43. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  44. private extern static MethodBase GetMethodFromHandleInternal(IntPtr handle);
  45. public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) {
  46. return GetMethodFromHandleInternal (handle.Value);
  47. }
  48. public abstract MethodImplAttributes GetMethodImplementationFlags();
  49. public abstract ParameterInfo[] GetParameters();
  50. //
  51. // This is a quick version for our own use. We should override
  52. // it where possible so that it does not allocate an array.
  53. //
  54. internal virtual int GetParameterCount ()
  55. {
  56. ParameterInfo [] pi = GetParameters ();
  57. if (pi == null)
  58. return 0;
  59. return pi.Length;
  60. }
  61. [DebuggerHidden]
  62. [DebuggerStepThrough]
  63. #if NET_2_0 || BOOTSTRAP_NET_2_0
  64. virtual
  65. #endif
  66. public Object Invoke(Object obj, Object[] parameters) {
  67. return Invoke (obj, 0, null, parameters, null);
  68. }
  69. public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
  70. protected MethodBase()
  71. {
  72. }
  73. public abstract RuntimeMethodHandle MethodHandle { get; }
  74. public abstract MethodAttributes Attributes { get; }
  75. public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
  76. public Boolean IsPublic {
  77. get {
  78. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
  79. }
  80. }
  81. public Boolean IsPrivate {
  82. get {
  83. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
  84. }
  85. }
  86. public Boolean IsFamily {
  87. get {
  88. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
  89. }
  90. }
  91. public Boolean IsAssembly {
  92. get {
  93. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
  94. }
  95. }
  96. public Boolean IsFamilyAndAssembly {
  97. get {
  98. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
  99. }
  100. }
  101. public Boolean IsFamilyOrAssembly {
  102. get {
  103. return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
  104. }
  105. }
  106. public Boolean IsStatic {
  107. get {
  108. return (Attributes & MethodAttributes.Static) != 0;
  109. }
  110. }
  111. public Boolean IsFinal {
  112. get {
  113. return (Attributes & MethodAttributes.Final) != 0;
  114. }
  115. }
  116. public Boolean IsVirtual {
  117. get {
  118. return (Attributes & MethodAttributes.Virtual) != 0;
  119. }
  120. }
  121. public Boolean IsHideBySig {
  122. get {
  123. return (Attributes & MethodAttributes.HideBySig) != 0;
  124. }
  125. }
  126. public Boolean IsAbstract {
  127. get {
  128. return (Attributes & MethodAttributes.Abstract) != 0;
  129. }
  130. }
  131. public Boolean IsSpecialName {
  132. get {
  133. int attr = (int)Attributes;
  134. return (attr & (int)MethodAttributes.SpecialName) != 0;
  135. }
  136. }
  137. public Boolean IsConstructor {
  138. get {
  139. int attr = (int)Attributes;
  140. return ((attr & (int)MethodAttributes.RTSpecialName) != 0
  141. && (Name == ".ctor"));
  142. }
  143. }
  144. internal virtual int get_next_table_index (object obj, int table, bool inc) {
  145. if (this is MethodBuilder) {
  146. MethodBuilder mb = (MethodBuilder)this;
  147. return mb.get_next_table_index (obj, table, inc);
  148. }
  149. if (this is ConstructorBuilder) {
  150. ConstructorBuilder mb = (ConstructorBuilder)this;
  151. return mb.get_next_table_index (obj, table, inc);
  152. }
  153. throw new Exception ("Method is not a builder method");
  154. }
  155. #if NET_2_0 || BOOTSTRAP_NET_2_0
  156. public virtual MethodInfo BindGenericParameters (Type [] types)
  157. {
  158. throw new NotSupportedException ();
  159. }
  160. public virtual Type [] GetGenericArguments ()
  161. {
  162. throw new NotSupportedException ();
  163. }
  164. public virtual MethodInfo GetGenericMethodDefinition ()
  165. {
  166. throw new NotSupportedException ();
  167. }
  168. public virtual bool Mono_IsInflatedMethod {
  169. get {
  170. throw new NotSupportedException ();
  171. }
  172. }
  173. public virtual bool HasGenericParameters {
  174. get {
  175. throw new NotSupportedException ();
  176. }
  177. }
  178. public virtual bool IsGenericMethodDefinition {
  179. get {
  180. throw new NotSupportedException ();
  181. }
  182. }
  183. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  184. internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
  185. internal static MethodBody GetMethodBody (IntPtr handle) {
  186. MethodBody mb = GetMethodBodyInternal (handle);
  187. if (mb == null)
  188. throw new ArgumentException ("Only methods with IL bodies are supported.");
  189. else
  190. return mb;
  191. }
  192. public virtual MethodBody GetMethodBody () {
  193. throw new NotSupportedException ();
  194. }
  195. #endif
  196. }
  197. }