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
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
34 using System.Runtime.Serialization;
35 using System.Reflection.Emit;
36 using System.Security;
37 using System.Threading;
41 namespace System.Reflection {
43 internal struct MonoMethodInfo
47 internal MethodAttributes attrs;
48 internal MethodImplAttributes iattrs;
49 internal CallingConventions callconv;
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
57 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58 internal static extern UnmanagedMarshal get_retval_marshal (IntPtr handle);
62 * Note: most of this class needs to be duplicated for the contructor, since
63 * the .NET reflection class hierarchy is so broken.
66 internal class MonoMethod : MethodInfo, ISerializable
68 #pragma warning disable 649
69 internal IntPtr mhandle;
72 #pragma warning restore 649
74 internal MonoMethod () {
77 internal MonoMethod (RuntimeMethodHandle mhandle) {
78 this.mhandle = mhandle.Value;
81 [MethodImplAttribute(MethodImplOptions.InternalCall)]
82 internal static extern string get_name (MethodBase method);
84 [MethodImplAttribute(MethodImplOptions.InternalCall)]
85 internal static extern MonoMethod get_base_definition (MonoMethod method);
87 public override MethodInfo GetBaseDefinition ()
89 return get_base_definition (this);
92 #if NET_2_0 || BOOTSTRAP_NET_2_0
93 public override ParameterInfo ReturnParameter {
95 return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
100 public override Type ReturnType {
103 MonoMethodInfo.get_method_info (mhandle, out info);
107 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
109 return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
113 public override MethodImplAttributes GetMethodImplementationFlags() {
115 MonoMethodInfo.get_method_info (mhandle, out info);
119 public override ParameterInfo[] GetParameters() {
120 return MonoMethodInfo.get_parameter_info (mhandle);
124 * InternalInvoke() receives the parameters correctly converted by the
125 * binder to match the types of the method signature.
127 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128 internal extern Object InternalInvoke (Object obj, Object[] parameters, out Exception exc);
130 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
133 binder = Binder.DefaultBinder;
134 ParameterInfo[] pinfo = GetParameters ();
135 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
136 throw new ArgumentException ("parameters");
139 if (SecurityManager.SecurityEnabled) {
140 // sadly Attributes doesn't tell us which kind of security action this is so
141 // we must do it the hard way - and it also means that we can skip calling
142 // Attribute (which is another an icall)
143 SecurityManager.ReflectedLinkDemandInvoke (this);
148 if (ContainsGenericParameters)
149 throw new InvalidOperationException ("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.");
156 // The ex argument is used to distinguish exceptions thrown by the icall
157 // from the exceptions thrown by the called method (which need to be
158 // wrapped in TargetInvocationException).
159 o = InternalInvoke (obj, parameters, out exc);
161 } catch (ThreadAbortException) {
165 } catch (MethodAccessException) {
168 } catch (Exception e) {
169 throw new TargetInvocationException (e);
177 public override RuntimeMethodHandle MethodHandle {
178 get {return new RuntimeMethodHandle (mhandle);}
180 public override MethodAttributes Attributes {
183 MonoMethodInfo.get_method_info (mhandle, out info);
188 public override CallingConventions CallingConvention {
191 MonoMethodInfo.get_method_info (mhandle, out info);
192 return info.callconv;
196 public override Type ReflectedType {
201 public override Type DeclaringType {
204 MonoMethodInfo.get_method_info (mhandle, out info);
208 public override string Name {
212 return get_name (this);
216 public override bool IsDefined (Type attributeType, bool inherit) {
217 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
220 public override object[] GetCustomAttributes( bool inherit) {
221 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
223 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
224 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
227 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228 internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
230 internal object[] GetPseudoCustomAttributes ()
234 /* MS.NET doesn't report MethodImplAttribute */
237 MonoMethodInfo.get_method_info (mhandle, out info);
238 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
240 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
245 object[] attrs = new object [count];
248 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
249 attrs [count ++] = new PreserveSigAttribute ();
250 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
251 DllImportAttribute attr = GetDllImportAttribute (mhandle);
252 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
253 attr.PreserveSig = true;
254 attrs [count ++] = attr;
260 static bool ShouldPrintFullName (Type type) {
261 return type.IsClass && (!type.IsPointer ||
263 (!type.GetElementType ().IsPrimitive && !type.GetElementType ().IsNested));
265 !type.GetElementType ().IsPrimitive);
269 public override string ToString () {
270 StringBuilder sb = new StringBuilder ();
271 Type retType = ReturnType;
272 if (ShouldPrintFullName (retType))
273 sb.Append (retType.ToString ());
275 sb.Append (retType.Name);
278 #if NET_2_0 || BOOTSTRAP_NET_2_0
279 if (IsGenericMethod) {
280 Type[] gen_params = GetGenericArguments ();
282 for (int j = 0; j < gen_params.Length; j++) {
285 sb.Append (gen_params [j].Name);
291 ParameterInfo[] p = GetParameters ();
292 for (int i = 0; i < p.Length; ++i) {
295 Type pt = p[i].ParameterType;
296 bool byref = pt.IsByRef;
298 pt = pt.GetElementType ();
299 if (ShouldPrintFullName (pt))
300 sb.Append (pt.ToString ());
304 sb.Append (" ByRef");
306 if ((CallingConvention & CallingConventions.VarArgs) != 0) {
313 return sb.ToString ();
318 public void GetObjectData(SerializationInfo info, StreamingContext context)
321 Type[] genericArguments = IsGenericMethod && !IsGenericMethodDefinition
322 ? GetGenericArguments () : null;
323 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method, genericArguments);
325 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
329 #if NET_2_0 || BOOTSTRAP_NET_2_0
330 public override MethodInfo MakeGenericMethod (Type [] methodInstantiation)
332 if (methodInstantiation == null)
333 throw new ArgumentNullException ("methodInstantiation");
334 foreach (Type type in methodInstantiation)
336 throw new ArgumentNullException ();
338 MethodInfo ret = MakeGenericMethod_impl (methodInstantiation);
340 throw new ArgumentException (String.Format ("The method has {0} generic parameter(s) but {1} generic argument(s) were provided.", GetGenericArguments ().Length, methodInstantiation.Length));
344 [MethodImplAttribute(MethodImplOptions.InternalCall)]
345 extern MethodInfo MakeGenericMethod_impl (Type [] types);
347 [MethodImplAttribute(MethodImplOptions.InternalCall)]
348 public override extern Type [] GetGenericArguments ();
350 [MethodImplAttribute(MethodImplOptions.InternalCall)]
351 extern MethodInfo GetGenericMethodDefinition_impl ();
353 public override MethodInfo GetGenericMethodDefinition ()
355 MethodInfo res = GetGenericMethodDefinition_impl ();
357 throw new InvalidOperationException ();
362 public override extern bool IsGenericMethodDefinition {
363 [MethodImplAttribute(MethodImplOptions.InternalCall)]
367 public override extern bool IsGenericMethod {
368 [MethodImplAttribute(MethodImplOptions.InternalCall)]
372 public override bool ContainsGenericParameters {
374 if (IsGenericMethod) {
375 foreach (Type arg in GetGenericArguments ())
376 if (arg.ContainsGenericParameters)
379 return DeclaringType.ContainsGenericParameters;
385 public override MethodBody GetMethodBody () {
386 return GetMethodBody (mhandle);
391 internal class MonoCMethod : ConstructorInfo, ISerializable
393 #pragma warning disable 649
394 internal IntPtr mhandle;
397 #pragma warning restore 649
399 public override MethodImplAttributes GetMethodImplementationFlags() {
401 MonoMethodInfo.get_method_info (mhandle, out info);
405 public override ParameterInfo[] GetParameters() {
406 return MonoMethodInfo.get_parameter_info (mhandle);
410 * InternalInvoke() receives the parameters corretcly converted by the binder
411 * to match the types of the method signature.
413 [MethodImplAttribute(MethodImplOptions.InternalCall)]
414 internal extern Object InternalInvoke (Object obj, Object[] parameters, out Exception exc);
416 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
419 binder = Binder.DefaultBinder;
420 ParameterInfo[] pinfo = GetParameters ();
421 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
422 throw new ArgumentException ("parameters");
424 if (SecurityManager.SecurityEnabled) {
425 // sadly Attributes doesn't tell us which kind of security action this is so
426 // we must do it the hard way - and it also means that we can skip calling
427 // Attribute (which is another an icall)
428 SecurityManager.ReflectedLinkDemandInvoke (this);
432 if (obj == null && DeclaringType.ContainsGenericParameters)
433 throw new MemberAccessException ("Cannot create an instance of " + DeclaringType + " because Type.ContainsGenericParameters is true.");
436 if ((invokeAttr & BindingFlags.CreateInstance) != 0 && DeclaringType.IsAbstract) {
437 throw new MemberAccessException (String.Format ("Cannot create an instance of {0} because it is an abstract class", DeclaringType));
440 Exception exc = null;
444 o = InternalInvoke (obj, parameters, out exc);
446 } catch (MethodAccessException) {
449 } catch (Exception e) {
450 throw new TargetInvocationException (e);
455 return (obj == null) ? o : null;
458 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
459 return Invoke (null, invokeAttr, binder, parameters, culture);
462 public override RuntimeMethodHandle MethodHandle {
463 get {return new RuntimeMethodHandle (mhandle);}
465 public override MethodAttributes Attributes {
468 MonoMethodInfo.get_method_info (mhandle, out info);
473 public override CallingConventions CallingConvention {
476 MonoMethodInfo.get_method_info (mhandle, out info);
477 return info.callconv;
481 public override Type ReflectedType {
486 public override Type DeclaringType {
489 MonoMethodInfo.get_method_info (mhandle, out info);
493 public override string Name {
497 return MonoMethod.get_name (this);
501 public override bool IsDefined (Type attributeType, bool inherit) {
502 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
505 public override object[] GetCustomAttributes( bool inherit) {
506 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
509 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
510 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
514 public override MethodBody GetMethodBody () {
515 return GetMethodBody (mhandle);
519 public override string ToString () {
520 StringBuilder sb = new StringBuilder ();
524 ParameterInfo[] p = GetParameters ();
525 for (int i = 0; i < p.Length; ++i) {
528 sb.Append (p[i].ParameterType.Name);
531 return sb.ToString ();
535 public void GetObjectData(SerializationInfo info, StreamingContext context)
537 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);