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)]
45 public abstract class MethodBase: MemberInfo, _MethodBase {
47 [MethodImplAttribute (MethodImplOptions.InternalCall)]
48 public extern static MethodBase GetCurrentMethod ();
50 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
52 return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
55 static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
57 if (handle == IntPtr.Zero)
58 throw new ArgumentException ("The handle is invalid.");
59 MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
61 throw new ArgumentException ("The handle is invalid.");
65 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
67 MethodBase res = GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
68 Type t = res.DeclaringType;
69 if (t.IsGenericType || t.IsGenericTypeDefinition)
70 throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
74 [MethodImplAttribute (MethodImplOptions.InternalCall)]
75 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
78 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
80 return GetMethodFromIntPtr (handle.Value, declaringType.Value);
83 public abstract MethodImplAttributes GetMethodImplementationFlags();
85 public abstract ParameterInfo[] GetParameters();
88 // This is a quick version for our own use. We should override
89 // it where possible so that it does not allocate an array.
91 internal virtual int GetParameterCount ()
93 throw new NotImplementedException ("must be implemented");
96 internal virtual Type GetParameterType (int pos) {
97 throw new NotImplementedException ();
101 [DebuggerStepThrough]
102 public Object Invoke(Object obj, Object[] parameters) {
103 return Invoke (obj, 0, null, parameters, null);
106 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
108 protected MethodBase()
112 public abstract RuntimeMethodHandle MethodHandle { get; }
113 public abstract MethodAttributes Attributes { get; }
114 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
115 public Boolean IsPublic {
117 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
120 public Boolean IsPrivate {
122 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
125 public Boolean IsFamily {
127 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
130 public Boolean IsAssembly {
132 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
135 public Boolean IsFamilyAndAssembly {
137 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
140 public Boolean IsFamilyOrAssembly {
142 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
145 public Boolean IsStatic {
147 return (Attributes & MethodAttributes.Static) != 0;
150 public Boolean IsFinal {
152 return (Attributes & MethodAttributes.Final) != 0;
155 public Boolean IsVirtual {
157 return (Attributes & MethodAttributes.Virtual) != 0;
160 public Boolean IsHideBySig {
162 return (Attributes & MethodAttributes.HideBySig) != 0;
165 public Boolean IsAbstract {
167 return (Attributes & MethodAttributes.Abstract) != 0;
170 public Boolean IsSpecialName {
172 int attr = (int)Attributes;
173 return (attr & (int)MethodAttributes.SpecialName) != 0;
176 [ComVisibleAttribute (true)]
177 public Boolean IsConstructor {
179 int attr = (int)Attributes;
180 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
181 && (Name == ".ctor"));
185 internal virtual int get_next_table_index (object obj, int table, bool inc) {
186 #if !FULL_AOT_RUNTIME
187 if (this is MethodBuilder) {
188 MethodBuilder mb = (MethodBuilder)this;
189 return mb.get_next_table_index (obj, table, inc);
191 if (this is ConstructorBuilder) {
192 ConstructorBuilder mb = (ConstructorBuilder)this;
193 return mb.get_next_table_index (obj, table, inc);
196 throw new Exception ("Method is not a builder method");
200 public virtual Type [] GetGenericArguments ()
202 throw new NotSupportedException ();
205 public virtual bool ContainsGenericParameters {
211 public virtual bool IsGenericMethodDefinition {
217 public virtual bool IsGenericMethod {
223 [MethodImplAttribute (MethodImplOptions.InternalCall)]
224 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
226 internal static MethodBody GetMethodBody (IntPtr handle) {
227 return GetMethodBodyInternal (handle);
230 public virtual MethodBody GetMethodBody () {
231 throw new NotSupportedException ();
235 public override bool Equals (object obj)
237 return obj == (object) this;
240 public override int GetHashCode ()
242 return base.GetHashCode ();
245 public static bool operator == (MethodBase left, MethodBase right)
247 if ((object)left == (object)right)
249 if ((object)left == null ^ (object)right == null)
251 return left.Equals (right);
254 public static bool operator != (MethodBase left, MethodBase right)
256 if ((object)left == (object)right)
258 if ((object)left == null ^ (object)right == null)
260 return !left.Equals (right);
263 public virtual bool IsSecurityCritical {
265 throw new NotImplementedException ();
269 public virtual bool IsSecuritySafeCritical {
271 throw new NotImplementedException ();
275 public virtual bool IsSecurityTransparent {
277 throw new NotImplementedException ();
282 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
284 throw new NotImplementedException ();
287 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
289 throw new NotImplementedException ();
292 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
294 throw new NotImplementedException ();
297 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
299 throw new NotImplementedException ();