2003-12-17 Zoltan Varga <vargaz@freemail.hu>
[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 using System;
12 using System.Globalization;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 using System.Runtime.Serialization;
16
17 namespace System.Reflection {
18         
19         internal struct MonoMethodInfo 
20         {
21                 internal Type parent;
22                 internal Type ret;
23                 internal MethodAttributes attrs;
24                 internal MethodImplAttributes iattrs;
25                 internal CallingConventions callconv;
26
27                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
28                 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
29                 
30                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
31                 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
32         };
33         
34         /*
35          * Note: most of this class needs to be duplicated for the contructor, since
36          * the .NET reflection class hierarchy is so broken.
37          */
38         [Serializable()]
39         internal class MonoMethod : MethodInfo, ISerializable\r
40         {
41                 internal IntPtr mhandle;
42                 string name;
43                 Type reftype;
44
45                 internal MonoMethod () {
46                 }
47
48                 internal MonoMethod (RuntimeMethodHandle mhandle) {
49                         this.mhandle = mhandle.Value;
50                 }
51                 
52                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
53                 internal static extern MonoMethod get_base_definition (MonoMethod method);
54
55                 public override MethodInfo GetBaseDefinition ()
56                 {
57                         return get_base_definition (this);
58                 }
59
60                 public override Type ReturnType {
61                         get {
62                                 MonoMethodInfo info;
63                                 MonoMethodInfo.get_method_info (mhandle, out info);
64                                 return info.ret;
65                         }
66                 }
67                 public override ICustomAttributeProvider ReturnTypeCustomAttributes { 
68                         get {
69                                 return new ParameterInfo (ReturnType, this);
70                         }
71                 }
72                 
73                 public override MethodImplAttributes GetMethodImplementationFlags() {
74                         MonoMethodInfo info;
75                         MonoMethodInfo.get_method_info (mhandle, out info);
76                         return info.iattrs;
77                 }
78
79                 public override ParameterInfo[] GetParameters() {
80                         return MonoMethodInfo.get_parameter_info (mhandle);
81                 }
82
83                 /*
84                  * InternalInvoke() receives the parameters corretcly converted by the binder
85                  * to match the types of the method signature.
86                  */
87                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
88                 internal extern Object InternalInvoke (Object obj, Object[] parameters);
89                 
90                 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
91                         if (binder == null)
92                                 binder = Binder.DefaultBinder;
93                         ParameterInfo[] pinfo = GetParameters ();
94                         if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
95                                 throw new ArgumentException ("parameters");
96                         try {
97                                 return InternalInvoke (obj, parameters);
98                         } catch (TargetException) {
99                                 throw;
100                         } catch (Exception e) {
101                                 throw new TargetInvocationException (e);
102                         }
103                 }
104
105                 public override RuntimeMethodHandle MethodHandle { 
106                         get {return new RuntimeMethodHandle (mhandle);} 
107                 }
108                 public override MethodAttributes Attributes { 
109                         get {
110                                 MonoMethodInfo info;
111                                 MonoMethodInfo.get_method_info (mhandle, out info);
112                                 return info.attrs;
113                         } 
114                 }
115
116                 public override CallingConventions CallingConvention { 
117                         get {
118                                 MonoMethodInfo info;
119                                 MonoMethodInfo.get_method_info (mhandle, out info);
120                                 return info.callconv;
121                         }
122                 }
123                 
124                 public override Type ReflectedType {
125                         get {
126                                 return reftype;
127                         }
128                 }
129                 public override Type DeclaringType {
130                         get {
131                                 MonoMethodInfo info;
132                                 MonoMethodInfo.get_method_info (mhandle, out info);
133                                 return info.parent;
134                         }
135                 }
136                 public override string Name {
137                         get {
138                                 return name;
139                         }
140                 }
141                 
142                 public override bool IsDefined (Type attributeType, bool inherit) {
143                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
144                 }
145
146                 public override object[] GetCustomAttributes( bool inherit) {
147                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
148                 }
149                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
150                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
151                 }
152
153                 public override string ToString () {
154                         string parms = "";
155                         ParameterInfo[] p = GetParameters ();
156                         for (int i = 0; i < p.Length; ++i) {
157                                 if (i > 0)
158                                         parms = parms + ", ";
159                                 if (p[i].ParameterType.IsClass)
160                                         parms = parms + p[i].ParameterType.Namespace + "." + p[i].ParameterType.Name;
161                                 else
162                                         parms = parms + p[i].ParameterType.Name;
163                         }
164                         if (ReturnType.IsClass) {
165                                 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
166                         }
167                         return ReturnType.Name + " " + Name + "(" + parms + ")";
168                 }
169
170         \r
171                 // ISerializable\r
172                 public void GetObjectData(SerializationInfo info, StreamingContext context) \r
173                 {\r
174                         ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);\r
175                 }\r
176
177 #if NET_1_2
178                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
179                 public override extern Type [] GetGenericArguments ();
180 #endif
181         }
182         
183         internal class MonoCMethod : ConstructorInfo, ISerializable\r
184         {
185                 internal IntPtr mhandle;
186                 string name;
187                 Type reftype;
188                 
189                 public override MethodImplAttributes GetMethodImplementationFlags() {
190                         MonoMethodInfo info;
191                         MonoMethodInfo.get_method_info (mhandle, out info);
192                         return info.iattrs;
193                 }
194
195                 public override ParameterInfo[] GetParameters() {
196                         return MonoMethodInfo.get_parameter_info (mhandle);
197                 }
198
199                 /*
200                  * InternalInvoke() receives the parameters corretcly converted by the binder
201                  * to match the types of the method signature.
202                  */
203                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
204                 internal extern Object InternalInvoke (Object obj, Object[] parameters);
205                 
206                 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
207                         if (binder == null)
208                                 binder = Binder.DefaultBinder;
209                         ParameterInfo[] pinfo = GetParameters ();
210                         if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
211                                 throw new ArgumentException ("parameters");
212                         try {
213                                 return InternalInvoke (obj, parameters);
214                         } catch (TargetException) {
215                                 throw;
216                         } catch (Exception e) {
217                                 throw new TargetInvocationException (e);
218                         }
219                 }
220
221                 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
222                         return Invoke (null, invokeAttr, binder, parameters, culture);
223                 }
224
225                 public override RuntimeMethodHandle MethodHandle { 
226                         get {return new RuntimeMethodHandle (mhandle);} 
227                 }
228                 public override MethodAttributes Attributes { 
229                         get {
230                                 MonoMethodInfo info;
231                                 MonoMethodInfo.get_method_info (mhandle, out info);
232                                 return info.attrs;
233                         } 
234                 }
235
236                 public override CallingConventions CallingConvention { 
237                         get {
238                                 MonoMethodInfo info;
239                                 MonoMethodInfo.get_method_info (mhandle, out info);
240                                 return info.callconv;
241                         }
242                 }
243                 
244                 public override Type ReflectedType {
245                         get {
246                                 return reftype;
247                         }
248                 }
249                 public override Type DeclaringType {
250                         get {
251                                 MonoMethodInfo info;
252                                 MonoMethodInfo.get_method_info (mhandle, out info);
253                                 return info.parent;
254                         }
255                 }
256                 public override string Name {
257                         get {
258                                 return name;
259                         }
260                 }
261
262                 public override bool IsDefined (Type attributeType, bool inherit) {
263                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
264                 }
265
266                 public override object[] GetCustomAttributes( bool inherit) {
267                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
268                 }
269
270                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
271                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
272                 }
273
274                 public override string ToString () {
275                         string parms = "";
276                         ParameterInfo[] p = GetParameters ();
277                         for (int i = 0; i < p.Length; ++i) {
278                                 if (i > 0)
279                                         parms = parms + ", ";
280                                 parms = parms + p [i].ParameterType.Name;
281                         }
282                         return "Void "+Name+"("+parms+")";
283                 }
284 \r
285                 // ISerializable\r
286                 public void GetObjectData(SerializationInfo info, StreamingContext context) \r
287                 {\r
288                         ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);\r
289                 }\r
290         }
291 }