MethodBase.cs 6.2 KB

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