2 // System.Reflection/MethodBase.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Diagnostics;
32 using System.Globalization;
34 using System.Reflection.Emit;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
39 namespace System.Reflection {
42 [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
44 [ClassInterface(ClassInterfaceType.None)]
46 public abstract class MethodBase: MemberInfo {
48 public abstract class MethodBase: MemberInfo, _MethodBase {
50 [MethodImplAttribute (MethodImplOptions.InternalCall)]
51 public extern static MethodBase GetCurrentMethod ();
53 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
55 return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
58 static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
60 if (handle == IntPtr.Zero)
61 throw new ArgumentException ("The handle is invalid.");
62 MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
64 throw new ArgumentException ("The handle is invalid.");
68 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
70 MethodBase res = GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
71 Type t = res.DeclaringType;
72 if (t.IsGenericType || t.IsGenericTypeDefinition)
73 throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
77 [MethodImplAttribute (MethodImplOptions.InternalCall)]
78 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
81 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
83 return GetMethodFromIntPtr (handle.Value, declaringType.Value);
86 public abstract MethodImplAttributes GetMethodImplementationFlags();
88 public abstract ParameterInfo[] GetParameters();
91 // This is a quick version for our own use. We should override
92 // it where possible so that it does not allocate an array.
93 // They cannot be abstract otherwise we break public contract
95 internal virtual ParameterInfo[] GetParametersInternal ()
98 return GetParameters ();
101 internal virtual int GetParametersCount ()
104 return GetParametersInternal ().Length;
107 internal virtual Type GetParameterType (int pos) {
108 throw new NotImplementedException ();
112 [DebuggerStepThrough]
113 public Object Invoke(Object obj, Object[] parameters) {
114 return Invoke (obj, 0, null, parameters, null);
117 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
119 protected MethodBase()
123 public abstract RuntimeMethodHandle MethodHandle { get; }
124 public abstract MethodAttributes Attributes { get; }
125 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
126 public Boolean IsPublic {
128 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
131 public Boolean IsPrivate {
133 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
136 public Boolean IsFamily {
138 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
141 public Boolean IsAssembly {
143 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
146 public Boolean IsFamilyAndAssembly {
148 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
151 public Boolean IsFamilyOrAssembly {
153 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
156 public Boolean IsStatic {
158 return (Attributes & MethodAttributes.Static) != 0;
161 public Boolean IsFinal {
163 return (Attributes & MethodAttributes.Final) != 0;
166 public Boolean IsVirtual {
168 return (Attributes & MethodAttributes.Virtual) != 0;
171 public Boolean IsHideBySig {
173 return (Attributes & MethodAttributes.HideBySig) != 0;
176 public Boolean IsAbstract {
178 return (Attributes & MethodAttributes.Abstract) != 0;
181 public Boolean IsSpecialName {
183 int attr = (int)Attributes;
184 return (attr & (int)MethodAttributes.SpecialName) != 0;
187 [ComVisibleAttribute (true)]
188 public Boolean IsConstructor {
190 int attr = (int)Attributes;
191 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
192 && (Name == ".ctor"));
196 internal virtual int get_next_table_index (object obj, int table, bool inc) {
197 #if !FULL_AOT_RUNTIME
198 if (this is MethodBuilder) {
199 MethodBuilder mb = (MethodBuilder)this;
200 return mb.get_next_table_index (obj, table, inc);
202 if (this is ConstructorBuilder) {
203 ConstructorBuilder mb = (ConstructorBuilder)this;
204 return mb.get_next_table_index (obj, table, inc);
207 throw new Exception ("Method is not a builder method");
211 public virtual Type [] GetGenericArguments ()
213 throw new NotSupportedException ();
216 public virtual bool ContainsGenericParameters {
222 public virtual bool IsGenericMethodDefinition {
228 public virtual bool IsGenericMethod {
234 [MethodImplAttribute (MethodImplOptions.InternalCall)]
235 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
237 internal static MethodBody GetMethodBody (IntPtr handle) {
238 return GetMethodBodyInternal (handle);
241 public virtual MethodBody GetMethodBody () {
242 throw new NotSupportedException ();
246 public override bool Equals (object obj)
248 return obj == (object) this;
251 public override int GetHashCode ()
253 return base.GetHashCode ();
256 public static bool operator == (MethodBase left, MethodBase right)
258 if ((object)left == (object)right)
260 if ((object)left == null ^ (object)right == null)
262 return left.Equals (right);
265 public static bool operator != (MethodBase left, MethodBase right)
267 if ((object)left == (object)right)
269 if ((object)left == null ^ (object)right == null)
271 return !left.Equals (right);
274 public virtual bool IsSecurityCritical {
276 throw new NotImplementedException ();
280 public virtual bool IsSecuritySafeCritical {
282 throw new NotImplementedException ();
286 public virtual bool IsSecurityTransparent {
288 throw new NotImplementedException ();
294 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
296 throw new NotImplementedException ();
299 Type _MethodBase.GetType ()
301 // Required or object::GetType becomes virtual final
302 return base.GetType ();
305 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
307 throw new NotImplementedException ();
310 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
312 throw new NotImplementedException ();
315 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
317 throw new NotImplementedException ();