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