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