2008-07-23 Marek Safar <marek.safar@gmail.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 #if NET_2_0
39         [ComVisible (true)]
40         [ComDefaultInterfaceAttribute (typeof (_MethodBase))]
41 #endif
42         [Serializable]
43         [ClassInterface(ClassInterfaceType.None)]
44         public abstract class MethodBase: MemberInfo, _MethodBase {
45
46                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
47                 public extern static MethodBase GetCurrentMethod ();
48
49                 internal static MethodBase GetMethodFromHandleNoGenericCheck (RuntimeMethodHandle handle) {
50                         if (handle.Value == IntPtr.Zero)
51                                 throw new ArgumentException ("The handle is invalid.");
52                         MethodBase res = GetMethodFromHandleInternalType (handle.Value, IntPtr.Zero);
53                         if (res == null)
54                                 throw new ArgumentException ("The handle is invalid.");                 
55                         return res;
56                 }
57
58                 public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle) {
59                         MethodBase res = GetMethodFromHandleNoGenericCheck (handle);
60 #if NET_2_0
61                         Type t = res.DeclaringType;
62                         if (t.IsGenericType || t.IsGenericTypeDefinition)
63                                 throw new ArgumentException ("Cannot resolve method because it's declared in a generic class.");
64 #endif
65                         return res;
66                 }
67
68                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
69                 private extern static MethodBase GetMethodFromHandleInternalType (IntPtr method_handle, IntPtr type_handle);
70
71 #if NET_2_0 || BOOTSTRAP_NET_2_0
72                 [ComVisible (false)]
73                 public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)
74                 {
75                         if (handle.Value == IntPtr.Zero)
76                                 throw new ArgumentException ("The handle is invalid.");
77                         MethodBase res = GetMethodFromHandleInternalType (handle.Value, declaringType.Value);
78                         if (res == null)
79                                 throw new ArgumentException ("The handle is invalid.");
80                         return res;
81                 }
82 #endif
83
84                 public abstract MethodImplAttributes GetMethodImplementationFlags();
85
86                 public abstract ParameterInfo[] GetParameters();
87                 
88                 //
89                 // This is a quick version for our own use. We should override
90                 // it where possible so that it does not allocate an array.
91                 //
92                 internal virtual int GetParameterCount ()
93                 {
94                         ParameterInfo [] pi = GetParameters ();
95                         if (pi == null)
96                                 return 0;
97                         
98                         return pi.Length;
99                 }
100
101 #if ONLY_1_1
102                 public new Type GetType ()
103                 {
104                         return base.GetType ();
105                 }
106 #endif
107
108                 [DebuggerHidden]
109                 [DebuggerStepThrough]           
110                 public Object Invoke(Object obj, Object[] parameters) {
111                         return Invoke (obj, 0, null, parameters, null);
112                 }
113
114                 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
115
116                 protected MethodBase()
117                 {
118                 }
119
120                 public abstract RuntimeMethodHandle MethodHandle { get; }
121                 public abstract MethodAttributes Attributes { get; }
122                 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
123                 public Boolean IsPublic { 
124                         get {
125                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
126                         }
127                 }
128                 public Boolean IsPrivate {
129                         get {
130                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
131                         }
132                 }
133                 public Boolean IsFamily {
134                         get {
135                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
136                         }
137                 }
138                 public Boolean IsAssembly {
139                         get {
140                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
141                         }
142                 }
143                 public Boolean IsFamilyAndAssembly {
144                         get {
145                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
146                         }
147                 }
148                 public Boolean IsFamilyOrAssembly {
149                         get {
150                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
151                         }
152                 }
153                 public Boolean IsStatic {
154                         get {
155                                 return (Attributes & MethodAttributes.Static) != 0;
156                         }
157                 }
158                 public Boolean IsFinal {
159                         get {
160                                 return (Attributes & MethodAttributes.Final) != 0;
161                         }
162                 }
163                 public Boolean IsVirtual {
164                         get {
165                                 return (Attributes & MethodAttributes.Virtual) != 0;
166                         }
167                 }
168                 public Boolean IsHideBySig {
169                         get {
170                                 return (Attributes & MethodAttributes.HideBySig) != 0;
171                         }
172                 }
173                 public Boolean IsAbstract {
174                         get {
175                                 return (Attributes & MethodAttributes.Abstract) != 0;
176                         }
177                 }
178                 public Boolean IsSpecialName {
179                         get {
180                                 int attr = (int)Attributes;
181                                 return (attr & (int)MethodAttributes.SpecialName) != 0;
182                         }
183                 }
184                 [ComVisibleAttribute (true)]
185                 public Boolean IsConstructor {
186                         get {
187                                 int attr = (int)Attributes;
188                                 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
189                                         && (Name == ".ctor"));
190                         }
191                 }
192
193                 internal virtual int get_next_table_index (object obj, int table, bool inc) {
194                         if (this is MethodBuilder) {
195                                 MethodBuilder mb = (MethodBuilder)this;
196                                 return mb.get_next_table_index (obj, table, inc);
197                         }
198                         if (this is ConstructorBuilder) {
199                                 ConstructorBuilder mb = (ConstructorBuilder)this;
200                                 return mb.get_next_table_index (obj, table, inc);
201                         }
202                         throw new Exception ("Method is not a builder method");
203                 }
204
205 #if NET_2_0 || BOOTSTRAP_NET_2_0
206                 [ComVisible (true)]
207                 public virtual Type [] GetGenericArguments ()
208                 {
209                         throw new NotSupportedException ();
210                 }
211
212                 public virtual bool ContainsGenericParameters {
213                         get {
214                                 return false;
215                         }
216                 }
217
218                 public virtual bool IsGenericMethodDefinition {
219                         get {
220                                 return false;
221                         }
222                 }
223
224                 public virtual bool IsGenericMethod {
225                         get {
226                                 return false;
227                         }
228                 }
229 #endif
230
231 #if NET_2_0
232                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
233                 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
234
235                 internal static MethodBody GetMethodBody (IntPtr handle) {
236                         return GetMethodBodyInternal (handle);
237                 }
238
239                 public virtual MethodBody GetMethodBody () {
240                         throw new NotSupportedException ();
241                 }
242 #endif
243
244                 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
245                 {
246                         throw new NotImplementedException ();
247                 }
248
249                 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
250                 {
251                         throw new NotImplementedException ();
252                 }
253
254                 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
255                 {
256                         throw new NotImplementedException ();
257                 }
258
259                 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
260                 {
261                         throw new NotImplementedException ();
262                 }
263         }
264 }