Merge pull request #1636 from alexrp/profiler-stuff
[mono.git] / mcs / class / corlib / System.Reflection / MethodBase.cs
1 //
2 // System.Reflection/MethodBase.cs
3 //
4 // Author:
5 //   Paolo Molaro (lupus@ximian.com)
6 //
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).
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Diagnostics;
32 using System.Globalization;
33 #if !FULL_AOT_RUNTIME
34 using System.Reflection.Emit;
35 #endif
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38
39 namespace System.Reflection {
40
41         [ComVisible (true)]
42         [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
43         [Serializable]
44         [ClassInterface(ClassInterfaceType.None)]
45 #if MOBILE
46         public abstract class MethodBase: MemberInfo {
47 #else
48         public abstract class MethodBase: MemberInfo, _MethodBase {
49 #endif
50                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
51                 public extern static MethodBase GetCurrentMethod ();
52
53                 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
54                 {
55                         return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
56                 }
57
58                 static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
59                 {
60                         if (handle == IntPtr.Zero)
61                                 throw new ArgumentException ("The handle is invalid.");
62                         MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
63                         if (res == null)
64                                 throw new ArgumentException ("The handle is invalid.");                 
65                         return res;
66                 }
67
68                 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
69                 {
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.");
74                         return res;
75                 }
76
77                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
78                 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
79
80                 [ComVisible (false)]
81                 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
82                 {
83                         return GetMethodFromIntPtr (handle.Value, declaringType.Value);
84                 }
85
86                 public abstract MethodImplAttributes GetMethodImplementationFlags();
87
88                 public abstract ParameterInfo[] GetParameters();
89                 
90                 //
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
94                 //
95                 internal virtual ParameterInfo[] GetParametersInternal ()
96                 {
97                         // Override me
98                         return GetParameters ();
99                 }
100
101                 internal ParameterInfo[] GetParametersNoCopy ()
102                 {
103                         return GetParametersInternal ();
104                 }
105
106                 internal virtual int GetParametersCount ()
107                 {
108                         // Override me
109                         return GetParametersInternal ().Length;
110                 }
111
112                 internal virtual Type GetParameterType (int pos) {
113                         throw new NotImplementedException ();
114                 }
115
116                 [DebuggerHidden]
117                 [DebuggerStepThrough]           
118                 public Object Invoke(Object obj, Object[] parameters) {
119                         return Invoke (obj, 0, null, parameters, null);
120                 }
121
122                 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
123
124                 protected MethodBase()
125                 {
126                 }
127
128                 public abstract RuntimeMethodHandle MethodHandle { get; }
129                 public abstract MethodAttributes Attributes { get; }
130                 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
131                 public Boolean IsPublic { 
132                         get {
133                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
134                         }
135                 }
136                 public Boolean IsPrivate {
137                         get {
138                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
139                         }
140                 }
141                 public Boolean IsFamily {
142                         get {
143                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
144                         }
145                 }
146                 public Boolean IsAssembly {
147                         get {
148                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
149                         }
150                 }
151                 public Boolean IsFamilyAndAssembly {
152                         get {
153                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
154                         }
155                 }
156                 public Boolean IsFamilyOrAssembly {
157                         get {
158                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
159                         }
160                 }
161                 public Boolean IsStatic {
162                         get {
163                                 return (Attributes & MethodAttributes.Static) != 0;
164                         }
165                 }
166                 public Boolean IsFinal {
167                         get {
168                                 return (Attributes & MethodAttributes.Final) != 0;
169                         }
170                 }
171                 public Boolean IsVirtual {
172                         get {
173                                 return (Attributes & MethodAttributes.Virtual) != 0;
174                         }
175                 }
176                 public Boolean IsHideBySig {
177                         get {
178                                 return (Attributes & MethodAttributes.HideBySig) != 0;
179                         }
180                 }
181                 public Boolean IsAbstract {
182                         get {
183                                 return (Attributes & MethodAttributes.Abstract) != 0;
184                         }
185                 }
186                 public Boolean IsSpecialName {
187                         get {
188                                 int attr = (int)Attributes;
189                                 return (attr & (int)MethodAttributes.SpecialName) != 0;
190                         }
191                 }
192                 [ComVisibleAttribute (true)]
193                 public Boolean IsConstructor {
194                         get {
195                                 int attr = (int)Attributes;
196                                 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
197                                         && (Name == ".ctor"));
198                         }
199                 }
200
201                 internal virtual int get_next_table_index (object obj, int table, bool inc) {
202 #if !FULL_AOT_RUNTIME
203                         if (this is MethodBuilder) {
204                                 MethodBuilder mb = (MethodBuilder)this;
205                                 return mb.get_next_table_index (obj, table, inc);
206                         }
207                         if (this is ConstructorBuilder) {
208                                 ConstructorBuilder mb = (ConstructorBuilder)this;
209                                 return mb.get_next_table_index (obj, table, inc);
210                         }
211 #endif
212                         throw new Exception ("Method is not a builder method");
213                 }
214
215                 [ComVisible (true)]
216                 public virtual Type [] GetGenericArguments ()
217                 {
218                         throw new NotSupportedException ();
219                 }
220
221                 public virtual bool ContainsGenericParameters {
222                         get {
223                                 return false;
224                         }
225                 }
226
227                 public virtual bool IsGenericMethodDefinition {
228                         get {
229                                 return false;
230                         }
231                 }
232
233                 public virtual bool IsGenericMethod {
234                         get {
235                                 return false;
236                         }
237                 }
238
239                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
240                 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
241
242                 internal static MethodBody GetMethodBody (IntPtr handle) {
243                         return GetMethodBodyInternal (handle);
244                 }
245
246                 public virtual MethodBody GetMethodBody () {
247                         throw new NotSupportedException ();
248                 }
249
250                 public override bool Equals (object obj)
251                 {
252                         return obj == (object) this;
253                 }
254
255                 public override int GetHashCode ()
256                 {
257                         return base.GetHashCode ();
258                 }
259
260                 public static bool operator == (MethodBase left, MethodBase right)
261                 {
262                         if ((object)left == (object)right)
263                                 return true;
264                         if ((object)left == null ^ (object)right == null)
265                                 return false;
266                         return left.Equals (right);
267                 }
268
269                 public static bool operator != (MethodBase left, MethodBase right)
270                 {
271                         if ((object)left == (object)right)
272                                 return false;
273                         if ((object)left == null ^ (object)right == null)
274                                 return true;
275                         return !left.Equals (right);
276                 }
277                 
278                 public virtual bool IsSecurityCritical {
279                         get {
280                                 throw new NotSupportedException ();
281                         }
282                 }
283                 
284                 public virtual bool IsSecuritySafeCritical {
285                         get {
286                                 throw new NotSupportedException ();
287                         }
288                 }
289
290                 public virtual bool IsSecurityTransparent {
291                         get {
292                                 throw new NotSupportedException ();
293                         }
294                 }
295
296                 public virtual MethodImplAttributes MethodImplementationFlags {
297                         get { return GetMethodImplementationFlags (); }
298                 }
299
300 #if !MOBILE
301                 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
302                 {
303                         throw new NotImplementedException ();
304                 }
305
306                 Type _MethodBase.GetType ()
307                 {
308                         // Required or object::GetType becomes virtual final
309                         return base.GetType ();
310                 }               
311
312                 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
313                 {
314                         throw new NotImplementedException ();
315                 }
316
317                 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
318                 {
319                         throw new NotImplementedException ();
320                 }
321
322                 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
323                 {
324                         throw new NotImplementedException ();
325                 }
326 #endif
327         }
328 }