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)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Diagnostics;
31 using System.Globalization;
32 using System.Reflection.Emit;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
36 namespace System.Reflection {
39 [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
41 [ClassInterface(ClassInterfaceType.None)]
42 public abstract class MethodBase: MemberInfo, _MethodBase {
44 [MethodImplAttribute (MethodImplOptions.InternalCall)]
45 public extern static MethodBase GetCurrentMethod ();
47 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
49 return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
52 static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
54 if (handle == IntPtr.Zero)
55 throw new ArgumentException ("The handle is invalid.");
56 MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
58 throw new ArgumentException ("The handle is invalid.");
62 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
64 MethodBase res = GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
65 Type t = res.DeclaringType;
66 if (t.IsGenericType || t.IsGenericTypeDefinition)
67 throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
71 [MethodImplAttribute (MethodImplOptions.InternalCall)]
72 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
75 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
77 return GetMethodFromIntPtr (handle.Value, declaringType.Value);
80 public abstract MethodImplAttributes GetMethodImplementationFlags();
82 public abstract ParameterInfo[] GetParameters();
85 // This is a quick version for our own use. We should override
86 // it where possible so that it does not allocate an array.
88 internal virtual int GetParameterCount ()
90 throw new NotImplementedException ("must be implemented");
95 public Object Invoke(Object obj, Object[] parameters) {
96 return Invoke (obj, 0, null, parameters, null);
99 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
101 protected MethodBase()
105 public abstract RuntimeMethodHandle MethodHandle { get; }
106 public abstract MethodAttributes Attributes { get; }
107 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
108 public Boolean IsPublic {
110 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
113 public Boolean IsPrivate {
115 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
118 public Boolean IsFamily {
120 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
123 public Boolean IsAssembly {
125 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
128 public Boolean IsFamilyAndAssembly {
130 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
133 public Boolean IsFamilyOrAssembly {
135 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
138 public Boolean IsStatic {
140 return (Attributes & MethodAttributes.Static) != 0;
143 public Boolean IsFinal {
145 return (Attributes & MethodAttributes.Final) != 0;
148 public Boolean IsVirtual {
150 return (Attributes & MethodAttributes.Virtual) != 0;
153 public Boolean IsHideBySig {
155 return (Attributes & MethodAttributes.HideBySig) != 0;
158 public Boolean IsAbstract {
160 return (Attributes & MethodAttributes.Abstract) != 0;
163 public Boolean IsSpecialName {
165 int attr = (int)Attributes;
166 return (attr & (int)MethodAttributes.SpecialName) != 0;
169 [ComVisibleAttribute (true)]
170 public Boolean IsConstructor {
172 int attr = (int)Attributes;
173 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
174 && (Name == ".ctor"));
178 internal virtual int get_next_table_index (object obj, int table, bool inc) {
179 if (this is MethodBuilder) {
180 MethodBuilder mb = (MethodBuilder)this;
181 return mb.get_next_table_index (obj, table, inc);
183 if (this is ConstructorBuilder) {
184 ConstructorBuilder mb = (ConstructorBuilder)this;
185 return mb.get_next_table_index (obj, table, inc);
187 throw new Exception ("Method is not a builder method");
191 public virtual Type [] GetGenericArguments ()
193 throw new NotSupportedException ();
196 public virtual bool ContainsGenericParameters {
202 public virtual bool IsGenericMethodDefinition {
208 public virtual bool IsGenericMethod {
214 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
217 internal static MethodBody GetMethodBody (IntPtr handle) {
218 return GetMethodBodyInternal (handle);
221 public virtual MethodBody GetMethodBody () {
222 throw new NotSupportedException ();
227 public override bool Equals (object obj)
232 public override int GetHashCode ()
234 return base.GetHashCode ();
237 public static bool operator == (MethodBase left, MethodBase right)
239 if ((object)left == (object)right)
241 if ((object)left == null ^ (object)right == null)
243 return left.Equals (right);
246 public static bool operator != (MethodBase left, MethodBase right)
248 if ((object)left == (object)right)
250 if ((object)left == null ^ (object)right == null)
252 return !left.Equals (right);
256 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
258 throw new NotImplementedException ();
261 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
263 throw new NotImplementedException ();
266 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
268 throw new NotImplementedException ();
271 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
273 throw new NotImplementedException ();