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 ParameterInfo [] pi = GetParameters ();
98 public new Type GetType ()
100 return base.GetType ();
105 [DebuggerStepThrough]
106 public Object Invoke(Object obj, Object[] parameters) {
107 return Invoke (obj, 0, null, parameters, null);
110 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
112 protected MethodBase()
116 public abstract RuntimeMethodHandle MethodHandle { get; }
117 public abstract MethodAttributes Attributes { get; }
118 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
119 public Boolean IsPublic {
121 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
124 public Boolean IsPrivate {
126 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
129 public Boolean IsFamily {
131 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
134 public Boolean IsAssembly {
136 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
139 public Boolean IsFamilyAndAssembly {
141 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
144 public Boolean IsFamilyOrAssembly {
146 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
149 public Boolean IsStatic {
151 return (Attributes & MethodAttributes.Static) != 0;
154 public Boolean IsFinal {
156 return (Attributes & MethodAttributes.Final) != 0;
159 public Boolean IsVirtual {
161 return (Attributes & MethodAttributes.Virtual) != 0;
164 public Boolean IsHideBySig {
166 return (Attributes & MethodAttributes.HideBySig) != 0;
169 public Boolean IsAbstract {
171 return (Attributes & MethodAttributes.Abstract) != 0;
174 public Boolean IsSpecialName {
176 int attr = (int)Attributes;
177 return (attr & (int)MethodAttributes.SpecialName) != 0;
180 [ComVisibleAttribute (true)]
181 public Boolean IsConstructor {
183 int attr = (int)Attributes;
184 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
185 && (Name == ".ctor"));
189 internal virtual int get_next_table_index (object obj, int table, bool inc) {
190 if (this is MethodBuilder) {
191 MethodBuilder mb = (MethodBuilder)this;
192 return mb.get_next_table_index (obj, table, inc);
194 if (this is ConstructorBuilder) {
195 ConstructorBuilder mb = (ConstructorBuilder)this;
196 return mb.get_next_table_index (obj, table, inc);
198 throw new Exception ("Method is not a builder method");
202 public virtual Type [] GetGenericArguments ()
204 throw new NotSupportedException ();
207 public virtual bool ContainsGenericParameters {
213 public virtual bool IsGenericMethodDefinition {
219 public virtual bool IsGenericMethod {
225 [MethodImplAttribute (MethodImplOptions.InternalCall)]
226 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
228 internal static MethodBody GetMethodBody (IntPtr handle) {
229 return GetMethodBodyInternal (handle);
232 public virtual MethodBody GetMethodBody () {
233 throw new NotSupportedException ();
238 public override bool Equals (object obj)
243 public override int GetHashCode ()
245 return base.GetHashCode ();
248 public static bool operator == (MethodBase left, MethodBase right)
250 if ((object)left == (object)right)
252 if ((object)left == null ^ (object)right == null)
254 return left.Equals (right);
257 public static bool operator != (MethodBase left, MethodBase right)
259 if ((object)left == (object)right)
261 if ((object)left == null ^ (object)right == null)
263 return !left.Equals (right);
267 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
269 throw new NotImplementedException ();
272 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
274 throw new NotImplementedException ();
277 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
279 throw new NotImplementedException ();
282 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
284 throw new NotImplementedException ();