2 // System.Reflection/MonoMethod.cs
3 // The class used to represent methods from the mono runtime.
6 // Paolo Molaro (lupus@ximian.com)
8 // (C) 2001 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Globalization;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Runtime.Serialization;
40 namespace System.Reflection {
42 internal struct MonoMethodInfo
46 internal MethodAttributes attrs;
47 internal MethodImplAttributes iattrs;
48 internal CallingConventions callconv;
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
53 [MethodImplAttribute(MethodImplOptions.InternalCall)]
54 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
58 * Note: most of this class needs to be duplicated for the contructor, since
59 * the .NET reflection class hierarchy is so broken.
62 internal class MonoMethod : MethodInfo, ISerializable
\r
64 internal IntPtr mhandle;
68 internal MonoMethod () {
71 internal MonoMethod (RuntimeMethodHandle mhandle) {
72 this.mhandle = mhandle.Value;
75 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76 internal static extern MonoMethod get_base_definition (MonoMethod method);
78 public override MethodInfo GetBaseDefinition ()
80 return get_base_definition (this);
83 public override Type ReturnType {
86 MonoMethodInfo.get_method_info (mhandle, out info);
90 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
92 return new ParameterInfo (ReturnType, this);
96 public override MethodImplAttributes GetMethodImplementationFlags() {
98 MonoMethodInfo.get_method_info (mhandle, out info);
102 public override ParameterInfo[] GetParameters() {
103 return MonoMethodInfo.get_parameter_info (mhandle);
107 * InternalInvoke() receives the parameters corretcly converted by the binder
108 * to match the types of the method signature.
110 [MethodImplAttribute(MethodImplOptions.InternalCall)]
111 internal extern Object InternalInvoke (Object obj, Object[] parameters);
113 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
115 binder = Binder.DefaultBinder;
116 ParameterInfo[] pinfo = GetParameters ();
117 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
118 throw new ArgumentException ("parameters");
120 return InternalInvoke (obj, parameters);
121 } catch (TargetException) {
123 } catch (Exception e) {
124 throw new TargetInvocationException (e);
128 public override RuntimeMethodHandle MethodHandle {
129 get {return new RuntimeMethodHandle (mhandle);}
131 public override MethodAttributes Attributes {
134 MonoMethodInfo.get_method_info (mhandle, out info);
139 public override CallingConventions CallingConvention {
142 MonoMethodInfo.get_method_info (mhandle, out info);
143 return info.callconv;
147 public override Type ReflectedType {
152 public override Type DeclaringType {
155 MonoMethodInfo.get_method_info (mhandle, out info);
159 public override string Name {
165 public override bool IsDefined (Type attributeType, bool inherit) {
166 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
169 public override object[] GetCustomAttributes( bool inherit) {
170 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
172 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
173 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
176 public override string ToString () {
178 ParameterInfo[] p = GetParameters ();
179 for (int i = 0; i < p.Length; ++i) {
181 parms = parms + ", ";
182 Type pt = p[i].ParameterType;
183 if (pt.IsClass && pt.Namespace != "")
184 parms = parms + pt.Namespace + "." + pt.Name;
186 parms = parms + pt.Name;
188 if (ReturnType.IsClass && ReturnType.Namespace != "")
189 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
191 #if NET_2_0 || BOOTSTRAP_NET_2_0
192 if (HasGenericParameters) {
193 Type[] gen_params = GetGenericArguments ();
195 for (int j = 0; j < gen_params.Length; j++) {
198 generic += gen_params [j].Name;
203 return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
208 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
210 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
\r
213 #if NET_2_0 || BOOTSTRAP_NET_2_0
214 [MethodImplAttribute(MethodImplOptions.InternalCall)]
215 public override extern MethodInfo BindGenericParameters (Type [] types);
217 [MethodImplAttribute(MethodImplOptions.InternalCall)]
218 public override extern Type [] GetGenericArguments ();
220 [MethodImplAttribute(MethodImplOptions.InternalCall)]
221 extern MethodInfo GetGenericMethodDefinition_impl ();
223 public override MethodInfo GetGenericMethodDefinition ()
225 MethodInfo res = GetGenericMethodDefinition_impl ();
227 throw new InvalidOperationException ();
232 public override extern bool Mono_IsInflatedMethod {
233 [MethodImplAttribute(MethodImplOptions.InternalCall)]
237 public override extern bool HasGenericParameters {
238 [MethodImplAttribute(MethodImplOptions.InternalCall)]
242 public override extern bool IsGenericMethodDefinition {
243 [MethodImplAttribute(MethodImplOptions.InternalCall)]
249 internal class MonoCMethod : ConstructorInfo, ISerializable
\r
251 internal IntPtr mhandle;
255 public override MethodImplAttributes GetMethodImplementationFlags() {
257 MonoMethodInfo.get_method_info (mhandle, out info);
261 public override ParameterInfo[] GetParameters() {
262 return MonoMethodInfo.get_parameter_info (mhandle);
266 * InternalInvoke() receives the parameters corretcly converted by the binder
267 * to match the types of the method signature.
269 [MethodImplAttribute(MethodImplOptions.InternalCall)]
270 internal extern Object InternalInvoke (Object obj, Object[] parameters);
272 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
274 binder = Binder.DefaultBinder;
275 ParameterInfo[] pinfo = GetParameters ();
276 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
277 throw new ArgumentException ("parameters");
279 return InternalInvoke (obj, parameters);
280 } catch (TargetException) {
282 } catch (Exception e) {
283 throw new TargetInvocationException (e);
287 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
288 return Invoke (null, invokeAttr, binder, parameters, culture);
291 public override RuntimeMethodHandle MethodHandle {
292 get {return new RuntimeMethodHandle (mhandle);}
294 public override MethodAttributes Attributes {
297 MonoMethodInfo.get_method_info (mhandle, out info);
302 public override CallingConventions CallingConvention {
305 MonoMethodInfo.get_method_info (mhandle, out info);
306 return info.callconv;
310 public override Type ReflectedType {
315 public override Type DeclaringType {
318 MonoMethodInfo.get_method_info (mhandle, out info);
322 public override string Name {
328 public override bool IsDefined (Type attributeType, bool inherit) {
329 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
332 public override object[] GetCustomAttributes( bool inherit) {
333 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
336 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
337 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
340 #if NET_2_0 || BOOTSTRAP_NET_2_0
341 [MethodImplAttribute(MethodImplOptions.InternalCall)]
342 extern MethodInfo GetGenericMethodDefinition_impl ();
344 public override MethodInfo GetGenericMethodDefinition ()
346 MethodInfo res = GetGenericMethodDefinition_impl ();
348 throw new InvalidOperationException ();
353 public override extern bool Mono_IsInflatedMethod {
354 [MethodImplAttribute(MethodImplOptions.InternalCall)]
358 public override bool HasGenericParameters {
364 public override bool IsGenericMethodDefinition {
371 public override string ToString () {
373 ParameterInfo[] p = GetParameters ();
374 for (int i = 0; i < p.Length; ++i) {
376 parms = parms + ", ";
377 parms = parms + p [i].ParameterType.Name;
379 return "Void "+Name+"("+parms+")";
383 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
385 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
\r