Facilitate the merge
[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                         throw new NotImplementedException ("must be implemented");
91                 }
92
93                 [DebuggerHidden]
94                 [DebuggerStepThrough]           
95                 public Object Invoke(Object obj, Object[] parameters) {
96                         return Invoke (obj, 0, null, parameters, null);
97                 }
98
99                 public abstract Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture);
100
101                 protected MethodBase()
102                 {
103                 }
104
105                 public abstract RuntimeMethodHandle MethodHandle { get; }
106                 public abstract MethodAttributes Attributes { get; }
107                 public virtual CallingConventions CallingConvention { get {return CallingConventions.Standard;} }
108                 public Boolean IsPublic { 
109                         get {
110                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Public;
111                         }
112                 }
113                 public Boolean IsPrivate {
114                         get {
115                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
116                         }
117                 }
118                 public Boolean IsFamily {
119                         get {
120                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Family;
121                         }
122                 }
123                 public Boolean IsAssembly {
124                         get {
125                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.Assembly;
126                         }
127                 }
128                 public Boolean IsFamilyAndAssembly {
129                         get {
130                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamANDAssem;
131                         }
132                 }
133                 public Boolean IsFamilyOrAssembly {
134                         get {
135                                 return (Attributes & MethodAttributes.MemberAccessMask) == MethodAttributes.FamORAssem;
136                         }
137                 }
138                 public Boolean IsStatic {
139                         get {
140                                 return (Attributes & MethodAttributes.Static) != 0;
141                         }
142                 }
143                 public Boolean IsFinal {
144                         get {
145                                 return (Attributes & MethodAttributes.Final) != 0;
146                         }
147                 }
148                 public Boolean IsVirtual {
149                         get {
150                                 return (Attributes & MethodAttributes.Virtual) != 0;
151                         }
152                 }
153                 public Boolean IsHideBySig {
154                         get {
155                                 return (Attributes & MethodAttributes.HideBySig) != 0;
156                         }
157                 }
158                 public Boolean IsAbstract {
159                         get {
160                                 return (Attributes & MethodAttributes.Abstract) != 0;
161                         }
162                 }
163                 public Boolean IsSpecialName {
164                         get {
165                                 int attr = (int)Attributes;
166                                 return (attr & (int)MethodAttributes.SpecialName) != 0;
167                         }
168                 }
169                 [ComVisibleAttribute (true)]
170                 public Boolean IsConstructor {
171                         get {
172                                 int attr = (int)Attributes;
173                                 return ((attr & (int)MethodAttributes.RTSpecialName) != 0
174                                         && (Name == ".ctor"));
175                         }
176                 }
177
178                 internal virtual int get_next_table_index (object obj, int table, bool inc) {
179                         if (this is MethodBuilder) {
180                                 MethodBuilder mb = (MethodBuilder)this;
181                                 return mb.get_next_table_index (obj, table, inc);
182                         }
183                         if (this is ConstructorBuilder) {
184                                 ConstructorBuilder mb = (ConstructorBuilder)this;
185                                 return mb.get_next_table_index (obj, table, inc);
186                         }
187                         throw new Exception ("Method is not a builder method");
188                 }
189
190                 [ComVisible (true)]
191                 public virtual Type [] GetGenericArguments ()
192                 {
193                         throw new NotSupportedException ();
194                 }
195
196                 public virtual bool ContainsGenericParameters {
197                         get {
198                                 return false;
199                         }
200                 }
201
202                 public virtual bool IsGenericMethodDefinition {
203                         get {
204                                 return false;
205                         }
206                 }
207
208                 public virtual bool IsGenericMethod {
209                         get {
210                                 return false;
211                         }
212                 }
213
214                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
215                 internal extern static MethodBody GetMethodBodyInternal (IntPtr handle);
216
217                 internal static MethodBody GetMethodBody (IntPtr handle) {
218                         return GetMethodBodyInternal (handle);
219                 }
220
221                 public virtual MethodBody GetMethodBody () {
222                         throw new NotSupportedException ();
223                 }
224
225
226 #if NET_4_0
227                 public override bool Equals (object obj)
228                 {
229                         return obj == this;
230                 }
231
232                 public override int GetHashCode ()
233                 {
234                         return base.GetHashCode ();
235                 }
236
237                 public static bool operator == (MethodBase left, MethodBase right)
238                 {
239                         if ((object)left == (object)right)
240                                 return true;
241                         if ((object)left == null ^ (object)right == null)
242                                 return false;
243                         return left.Equals (right);
244                 }
245
246                 public static bool operator != (MethodBase left, MethodBase right)
247                 {
248                         if ((object)left == (object)right)
249                                 return false;
250                         if ((object)left == null ^ (object)right == null)
251                                 return true;
252                         return !left.Equals (right);
253                 }
254 #endif
255
256                 void _MethodBase.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
257                 {
258                         throw new NotImplementedException ();
259                 }
260
261                 void _MethodBase.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
262                 {
263                         throw new NotImplementedException ();
264                 }
265
266                 void _MethodBase.GetTypeInfoCount (out uint pcTInfo)
267                 {
268                         throw new NotImplementedException ();
269                 }
270
271                 void _MethodBase.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
272                 {
273                         throw new NotImplementedException ();
274                 }
275         }
276 }