Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[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 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System.Diagnostics;
31 using System.Globalization;
32 using System.Reflection.Emit;
33 using System.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35
36 namespace System.Reflection {
37
38         [ComVisible (true)]
39         [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
40         [Serializable]
41         [ClassInterface(ClassInterfaceType.None)]
42         public abstract class MethodBase: MemberInfo, _MethodBase {
43
44                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
45                 public extern static MethodBase GetCurrentMethod ();
46
47                 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle)
48                 {
49                         return GetMethodFromIntPtr (handle.Value, IntPtr.Zero);
50                 }
51
52                 static MethodBase GetMethodFromIntPtr (IntPtr handle, IntPtr declaringType)
53                 {
54                         if (handle == IntPtr.Zero)
55                                 throw new ArgumentException ("The handle is invalid.");
56                         MethodBase res = GetMethodFromHandleInternalType (handle, declaringType);
57                         if (res == null)
58                                 throw new ArgumentException ("The handle is invalid.");                 
59                         return res;
60                 }
61
62                 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle)
63                 {
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.");
68                         return res;
69                 }
70
71                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
72                 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
73
74                 [ComVisible (false)]
75                 public static MethodBase GetMethodFromHandle (RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
76                 {
77                         return GetMethodFromIntPtr (handle.Value, declaringType.Value);
78                 }
79
80                 public abstract MethodImplAttributes GetMethodImplementationFlags();
81
82                 public abstract ParameterInfo[] GetParameters();
83                 
84                 //
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.
87                 //
88                 internal virtual int GetParameterCount ()
89                 {
90                         ParameterInfo [] pi = GetParameters ();
91                         if (pi == null)
92                                 return 0;
93                         
94                         return pi.Length;
95                 }
96
97 #if ONLY_1_1
98                 public new Type GetType ()
99                 {
100                         return base.GetType ();
101                 }
102 #endif
103
104                 [DebuggerHidden]
105                 [DebuggerStepThrough]           
106                 public Object Invoke(Object obj, Object[] parameters) {
107                         return Invoke (obj, 0, null, parameters, null);
108                 }
109
110                 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
111
112                 protected MethodBase()
113                 {
114                 }
115
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 { 
120                         get {
121                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
122                         }
123                 }
124                 public Boolean IsPrivate {
125                         get {
126                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
127                         }
128                 }
129                 public Boolean IsFamily {
130                         get {
131                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
132                         }
133                 }
134                 public Boolean IsAssembly {
135                         get {
136                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
137                         }
138                 }
139                 public Boolean IsFamilyAndAssembly {
140                         get {
141                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
142                         }
143                 }
144                 public Boolean IsFamilyOrAssembly {
145                         get {
146                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
147                         }
148                 }
149                 public Boolean IsStatic {
150                         get {
151                                 return (Attributes & MethodAttributes.Static) != 0;
152                         }
153                 }
154                 public Boolean IsFinal {
155                         get {
156                                 return (Attributes & MethodAttributes.Final) != 0;
157                         }
158                 }
159                 public Boolean IsVirtual {
160                         get {
161                                 return (Attributes & MethodAttributes.Virtual) != 0;
162                         }
163                 }
164                 public Boolean IsHideBySig {
165                         get {
166                                 return (Attributes & MethodAttributes.HideBySig) != 0;
167                         }
168                 }
169                 public Boolean IsAbstract {
170                         get {
171                                 return (Attributes & MethodAttributes.Abstract) != 0;
172                         }
173                 }
174                 public Boolean IsSpecialName {
175                         get {
176                                 int attr = (int)Attributes;
177                                 return (attr & (int)MethodAttributes.SpecialName) != 0;
178                         }
179                 }
180                 [ComVisibleAttribute (true)]
181                 public Boolean IsConstructor {
182                         get {
183                                 int attr = (int)Attributes;
184                                 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
185                                         && (Name == ".ctor"));
186                         }
187                 }
188
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);
193                         }
194                         if (this is ConstructorBuilder) {
195                                 ConstructorBuilder mb = (ConstructorBuilder)this;
196                                 return mb.get_next_table_index (obj, table, inc);
197                         }
198                         throw new Exception ("Method is not a builder method");
199                 }
200
201                 [ComVisible (true)]
202                 public virtual Type [] GetGenericArguments ()
203                 {
204                         throw new NotSupportedException ();
205                 }
206
207                 public virtual bool ContainsGenericParameters {
208                         get {
209                                 return false;
210                         }
211                 }
212
213                 public virtual bool IsGenericMethodDefinition {
214                         get {
215                                 return false;
216                         }
217                 }
218
219                 public virtual bool IsGenericMethod {
220                         get {
221                                 return false;
222                         }
223                 }
224
225                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
226                 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
227
228                 internal static MethodBody GetMethodBody (IntPtr handle) {
229                         return GetMethodBodyInternal (handle);
230                 }
231
232                 public virtual MethodBody GetMethodBody () {
233                         throw new NotSupportedException ();
234                 }
235
236
237 #if NET_4_0
238                 public override bool Equals (object obj)
239                 {
240                         return obj == this;
241                 }
242
243                 public override int GetHashCode ()
244                 {
245                         return base.GetHashCode ();
246                 }
247
248                 public static bool operator == (MethodBase left, MethodBase right)
249                 {
250                         if ((object)left == (object)right)
251                                 return true;
252                         if ((object)left == null ^ (object)right == null)
253                                 return false;
254                         return left.Equals (right);
255                 }
256
257                 public static bool operator != (MethodBase left, MethodBase right)
258                 {
259                         if ((object)left == (object)right)
260                                 return false;
261                         if ((object)left == null ^ (object)right == null)
262                                 return true;
263                         return !left.Equals (right);
264                 }
265 #endif
266
267                 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
268                 {
269                         throw new NotImplementedException ();
270                 }
271
272                 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
273                 {
274                         throw new NotImplementedException ();
275                 }
276
277                 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
278                 {
279                         throw new NotImplementedException ();
280                 }
281
282                 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
283                 {
284                         throw new NotImplementedException ();
285                 }
286         }
287 }