Add license and copyright to all source files in corlib
[mono.git] / mcs / class / corlib / System.Reflection / MonoMethod.cs
1 //
2 // System.Reflection/MonoMethod.cs
3 // The class used to represent methods from the mono runtime.
4 //
5 // Author:
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8 // (C) 2001 Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Runtime.Serialization;
39
40 namespace System.Reflection {
41         
42         internal struct MonoMethodInfo 
43         {
44                 internal Type parent;
45                 internal Type ret;
46                 internal MethodAttributes attrs;
47                 internal MethodImplAttributes iattrs;
48                 internal CallingConventions callconv;
49
50                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51                 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
52                 
53                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
54                 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
55         };
56         
57         /*
58          * Note: most of this class needs to be duplicated for the contructor, since
59          * the .NET reflection class hierarchy is so broken.
60          */
61         [Serializable()]
62         internal class MonoMethod : MethodInfo, ISerializable\r
63         {
64                 internal IntPtr mhandle;
65                 string name;
66                 Type reftype;
67
68                 internal MonoMethod () {
69                 }
70
71                 internal MonoMethod (RuntimeMethodHandle mhandle) {
72                         this.mhandle = mhandle.Value;
73                 }
74                 
75                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76                 internal static extern MonoMethod get_base_definition (MonoMethod method);
77
78                 public override MethodInfo GetBaseDefinition ()
79                 {
80                         return get_base_definition (this);
81                 }
82
83                 public override Type ReturnType {
84                         get {
85                                 MonoMethodInfo info;
86                                 MonoMethodInfo.get_method_info (mhandle, out info);
87                                 return info.ret;
88                         }
89                 }
90                 public override ICustomAttributeProvider ReturnTypeCustomAttributes { 
91                         get {
92                                 return new ParameterInfo (ReturnType, this);
93                         }
94                 }
95                 
96                 public override MethodImplAttributes GetMethodImplementationFlags() {
97                         MonoMethodInfo info;
98                         MonoMethodInfo.get_method_info (mhandle, out info);
99                         return info.iattrs;
100                 }
101
102                 public override ParameterInfo[] GetParameters() {
103                         return MonoMethodInfo.get_parameter_info (mhandle);
104                 }
105
106                 /*
107                  * InternalInvoke() receives the parameters corretcly converted by the binder
108                  * to match the types of the method signature.
109                  */
110                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
111                 internal extern Object InternalInvoke (Object obj, Object[] parameters);
112                 
113                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
114                         if (binder == null)
115                                 binder = Binder.DefaultBinder;
116                         ParameterInfo[] pinfo = GetParameters ();
117                         if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
118                                 throw new ArgumentException ("parameters");
119                         try {
120                                 return InternalInvoke (obj, parameters);
121                         } catch (TargetException) {
122                                 throw;
123                         } catch (Exception e) {
124                                 throw new TargetInvocationException (e);
125                         }
126                 }
127
128                 public override RuntimeMethodHandle MethodHandle { 
129                         get {return new RuntimeMethodHandle (mhandle);} 
130                 }
131                 public override MethodAttributes Attributes { 
132                         get {
133                                 MonoMethodInfo info;
134                                 MonoMethodInfo.get_method_info (mhandle, out info);
135                                 return info.attrs;
136                         } 
137                 }
138
139                 public override CallingConventions CallingConvention { 
140                         get {
141                                 MonoMethodInfo info;
142                                 MonoMethodInfo.get_method_info (mhandle, out info);
143                                 return info.callconv;
144                         }
145                 }
146                 
147                 public override Type ReflectedType {
148                         get {
149                                 return reftype;
150                         }
151                 }
152                 public override Type DeclaringType {
153                         get {
154                                 MonoMethodInfo info;
155                                 MonoMethodInfo.get_method_info (mhandle, out info);
156                                 return info.parent;
157                         }
158                 }
159                 public override string Name {
160                         get {
161                                 return name;
162                         }
163                 }
164                 
165                 public override bool IsDefined (Type attributeType, bool inherit) {
166                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
167                 }
168
169                 public override object[] GetCustomAttributes( bool inherit) {
170                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
171                 }
172                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
173                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
174                 }
175
176                 public override string ToString () {
177                         string parms = "";
178                         ParameterInfo[] p = GetParameters ();
179                         for (int i = 0; i < p.Length; ++i) {
180                                 if (i > 0)
181                                         parms = parms + ", ";
182                                 Type pt = p[i].ParameterType;
183                                 if (pt.IsClass && pt.Namespace != "")
184                                         parms = parms + pt.Namespace + "." + pt.Name;
185                                 else
186                                         parms = parms + pt.Name;
187                         }
188                         if (ReturnType.IsClass && ReturnType.Namespace != "")
189                                 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
190                         string generic = "";
191 #if NET_2_0 || BOOTSTRAP_NET_2_0
192                         if (HasGenericParameters) {
193                                 Type[] gen_params = GetGenericArguments ();
194                                 generic = "[";
195                                 for (int j = 0; j < gen_params.Length; j++) {
196                                         if (j > 0)
197                                                 generic += ",";
198                                         generic += gen_params [j].Name;
199                                 }
200                                 generic += "]";
201                         }
202 #endif
203                         return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
204                 }
205
206         \r
207                 // ISerializable\r
208                 public void GetObjectData(SerializationInfo info, StreamingContext context) \r
209                 {\r
210                         ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);\r
211                 }\r
212
213 #if NET_2_0 || BOOTSTRAP_NET_2_0
214                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215                 public override extern MethodInfo BindGenericParameters (Type [] types);
216
217                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218                 public override extern Type [] GetGenericArguments ();
219
220                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
221                 extern MethodInfo GetGenericMethodDefinition_impl ();
222
223                 public override MethodInfo GetGenericMethodDefinition ()
224                 {
225                         MethodInfo res = GetGenericMethodDefinition_impl ();
226                         if (res == null)
227                                 throw new InvalidOperationException ();
228
229                         return res;
230                 }
231
232                 public override extern bool Mono_IsInflatedMethod {
233                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
234                         get;
235                 }
236
237                 public override extern bool HasGenericParameters {
238                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
239                         get;
240                 }
241
242                 public override extern bool IsGenericMethodDefinition {
243                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
244                         get;
245                 }
246 #endif
247         }
248         
249         internal class MonoCMethod : ConstructorInfo, ISerializable\r
250         {
251                 internal IntPtr mhandle;
252                 string name;
253                 Type reftype;
254                 
255                 public override MethodImplAttributes GetMethodImplementationFlags() {
256                         MonoMethodInfo info;
257                         MonoMethodInfo.get_method_info (mhandle, out info);
258                         return info.iattrs;
259                 }
260
261                 public override ParameterInfo[] GetParameters() {
262                         return MonoMethodInfo.get_parameter_info (mhandle);
263                 }
264
265                 /*
266                  * InternalInvoke() receives the parameters corretcly converted by the binder
267                  * to match the types of the method signature.
268                  */
269                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
270                 internal extern Object InternalInvoke (Object obj, Object[] parameters);
271                 
272                 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
273                         if (binder == null)
274                                 binder = Binder.DefaultBinder;
275                         ParameterInfo[] pinfo = GetParameters ();
276                         if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
277                                 throw new ArgumentException ("parameters");
278                         try {
279                                 return InternalInvoke (obj, parameters);
280                         } catch (TargetException) {
281                                 throw;
282                         } catch (Exception e) {
283                                 throw new TargetInvocationException (e);
284                         }
285                 }
286
287                 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
288                         return Invoke (null, invokeAttr, binder, parameters, culture);
289                 }
290
291                 public override RuntimeMethodHandle MethodHandle { 
292                         get {return new RuntimeMethodHandle (mhandle);} 
293                 }
294                 public override MethodAttributes Attributes { 
295                         get {
296                                 MonoMethodInfo info;
297                                 MonoMethodInfo.get_method_info (mhandle, out info);
298                                 return info.attrs;
299                         } 
300                 }
301
302                 public override CallingConventions CallingConvention { 
303                         get {
304                                 MonoMethodInfo info;
305                                 MonoMethodInfo.get_method_info (mhandle, out info);
306                                 return info.callconv;
307                         }
308                 }
309                 
310                 public override Type ReflectedType {
311                         get {
312                                 return reftype;
313                         }
314                 }
315                 public override Type DeclaringType {
316                         get {
317                                 MonoMethodInfo info;
318                                 MonoMethodInfo.get_method_info (mhandle, out info);
319                                 return info.parent;
320                         }
321                 }
322                 public override string Name {
323                         get {
324                                 return name;
325                         }
326                 }
327
328                 public override bool IsDefined (Type attributeType, bool inherit) {
329                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
330                 }
331
332                 public override object[] GetCustomAttributes( bool inherit) {
333                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
334                 }
335
336                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
337                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
338                 }
339
340 #if NET_2_0 || BOOTSTRAP_NET_2_0
341                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
342                 extern MethodInfo GetGenericMethodDefinition_impl ();
343
344                 public override MethodInfo GetGenericMethodDefinition ()
345                 {
346                         MethodInfo res = GetGenericMethodDefinition_impl ();
347                         if (res == null)
348                                 throw new InvalidOperationException ();
349
350                         return res;
351                 }
352
353                 public override extern bool Mono_IsInflatedMethod {
354                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
355                         get;
356                 }
357
358                 public override bool HasGenericParameters {
359                         get {
360                                 return false;
361                         }
362                 }
363
364                 public override bool IsGenericMethodDefinition {
365                         get {
366                                 return false;
367                         }
368                 }
369 #endif
370
371                 public override string ToString () {
372                         string parms = "";
373                         ParameterInfo[] p = GetParameters ();
374                         for (int i = 0; i < p.Length; ++i) {
375                                 if (i > 0)
376                                         parms = parms + ", ";
377                                 parms = parms + p [i].ParameterType.Name;
378                         }
379                         return "Void "+Name+"("+parms+")";
380                 }
381 \r
382                 // ISerializable\r
383                 public void GetObjectData(SerializationInfo info, StreamingContext context) \r
384                 {\r
385                         ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);\r
386                 }\r
387         }
388 }