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