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 internal IntPtr mhandle;
72 internal MonoMethod () {
75 internal MonoMethod (RuntimeMethodHandle mhandle) {
76 this.mhandle = mhandle.Value;
79 [MethodImplAttribute(MethodImplOptions.InternalCall)]
80 internal static extern string get_name (MethodBase method);
82 [MethodImplAttribute(MethodImplOptions.InternalCall)]
83 internal static extern MonoMethod get_base_definition (MonoMethod method);
85 public override MethodInfo GetBaseDefinition ()
87 return get_base_definition (this);
90 #if NET_2_0 || BOOTSTRAP_NET_2_0
91 public override ParameterInfo ReturnParameter {
93 return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
98 public override Type ReturnType {
101 MonoMethodInfo.get_method_info (mhandle, out info);
105 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
107 return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
111 public override MethodImplAttributes GetMethodImplementationFlags() {
113 MonoMethodInfo.get_method_info (mhandle, out info);
117 public override ParameterInfo[] GetParameters() {
118 return MonoMethodInfo.get_parameter_info (mhandle);
122 * InternalInvoke() receives the parameters correctly converted by the
123 * binder to match the types of the method signature.
125 [MethodImplAttribute(MethodImplOptions.InternalCall)]
126 internal extern Object InternalInvoke (Object obj, Object[] parameters, out Exception exc);
128 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
131 binder = Binder.DefaultBinder;
132 ParameterInfo[] pinfo = GetParameters ();
133 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
134 throw new ArgumentException ("parameters");
136 if (SecurityManager.SecurityEnabled) {
137 // sadly Attributes doesn't tell us which kind of security action this is so
138 // we must do it the hard way - and it also means that we can skip calling
139 // Attribute (which is another an icall)
140 SecurityManager.ReflectedLinkDemandInvoke (this);
144 if (ContainsGenericParameters)
145 throw new InvalidOperationException ("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.");
152 // The ex argument is used to distinguish exceptions thrown by the icall
153 // from the exceptions thrown by the called method (which need to be
154 // wrapped in TargetInvocationException).
155 o = InternalInvoke (obj, parameters, out exc);
157 } catch (ThreadAbortException) {
161 } catch (MethodAccessException) {
164 } catch (Exception e) {
165 throw new TargetInvocationException (e);
173 public override RuntimeMethodHandle MethodHandle {
174 get {return new RuntimeMethodHandle (mhandle);}
176 public override MethodAttributes Attributes {
179 MonoMethodInfo.get_method_info (mhandle, out info);
184 public override CallingConventions CallingConvention {
187 MonoMethodInfo.get_method_info (mhandle, out info);
188 return info.callconv;
192 public override Type ReflectedType {
197 public override Type DeclaringType {
200 MonoMethodInfo.get_method_info (mhandle, out info);
204 public override string Name {
208 return get_name (this);
212 public override bool IsDefined (Type attributeType, bool inherit) {
213 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
216 public override object[] GetCustomAttributes( bool inherit) {
217 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
219 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
220 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
223 [MethodImplAttribute(MethodImplOptions.InternalCall)]
224 internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
226 internal object[] GetPseudoCustomAttributes ()
230 /* MS.NET doesn't report MethodImplAttribute */
233 MonoMethodInfo.get_method_info (mhandle, out info);
234 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
236 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
241 object[] attrs = new object [count];
244 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
245 attrs [count ++] = new PreserveSigAttribute ();
246 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
247 DllImportAttribute attr = GetDllImportAttribute (mhandle);
248 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
249 attr.PreserveSig = true;
250 attrs [count ++] = attr;
256 public override string ToString () {
257 StringBuilder sb = new StringBuilder ();
258 if (ReturnType.IsClass)
259 sb.Append (ReturnType.ToString ());
261 sb.Append (ReturnType.Name);
264 #if NET_2_0 || BOOTSTRAP_NET_2_0
265 if (IsGenericMethod) {
266 Type[] gen_params = GetGenericArguments ();
268 for (int j = 0; j < gen_params.Length; j++) {
271 sb.Append (gen_params [j].Name);
277 ParameterInfo[] p = GetParameters ();
278 for (int i = 0; i < p.Length; ++i) {
281 Type pt = p[i].ParameterType;
282 bool byref = pt.IsByRef;
284 pt = pt.GetElementType ();
286 sb.Append (pt.ToString ());
290 sb.Append (" ByRef");
292 if ((CallingConvention & CallingConventions.VarArgs) != 0) {
299 return sb.ToString ();
304 public void GetObjectData(SerializationInfo info, StreamingContext context)
307 Type[] genericArguments = IsGenericMethod && !IsGenericMethodDefinition
308 ? GetGenericArguments () : null;
309 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method, genericArguments);
311 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
315 #if NET_2_0 || BOOTSTRAP_NET_2_0
316 public override MethodInfo MakeGenericMethod (Type [] methodInstantiation)
318 if (methodInstantiation == null)
319 throw new ArgumentNullException ("methodInstantiation");
320 foreach (Type type in methodInstantiation)
322 throw new ArgumentNullException ();
324 MethodInfo ret = MakeGenericMethod_impl (methodInstantiation);
326 throw new ArgumentException (String.Format ("The method has {0} generic parameter(s) but {1} generic argument(s) were provided.", GetGenericArguments ().Length, methodInstantiation.Length));
330 [MethodImplAttribute(MethodImplOptions.InternalCall)]
331 extern MethodInfo MakeGenericMethod_impl (Type [] types);
333 [MethodImplAttribute(MethodImplOptions.InternalCall)]
334 public override extern Type [] GetGenericArguments ();
336 [MethodImplAttribute(MethodImplOptions.InternalCall)]
337 extern MethodInfo GetGenericMethodDefinition_impl ();
339 public override MethodInfo GetGenericMethodDefinition ()
341 MethodInfo res = GetGenericMethodDefinition_impl ();
343 throw new InvalidOperationException ();
348 public override extern bool IsGenericMethodDefinition {
349 [MethodImplAttribute(MethodImplOptions.InternalCall)]
353 public override extern bool IsGenericMethod {
354 [MethodImplAttribute(MethodImplOptions.InternalCall)]
358 public override bool ContainsGenericParameters {
360 if (IsGenericMethod) {
361 foreach (Type arg in GetGenericArguments ())
362 if (arg.ContainsGenericParameters)
365 return DeclaringType.ContainsGenericParameters;
371 public override MethodBody GetMethodBody () {
372 return GetMethodBody (mhandle);
377 internal class MonoCMethod : ConstructorInfo, ISerializable
379 internal IntPtr mhandle;
383 public override MethodImplAttributes GetMethodImplementationFlags() {
385 MonoMethodInfo.get_method_info (mhandle, out info);
389 public override ParameterInfo[] GetParameters() {
390 return MonoMethodInfo.get_parameter_info (mhandle);
394 * InternalInvoke() receives the parameters corretcly converted by the binder
395 * to match the types of the method signature.
397 [MethodImplAttribute(MethodImplOptions.InternalCall)]
398 internal extern Object InternalInvoke (Object obj, Object[] parameters, out Exception exc);
400 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
403 binder = Binder.DefaultBinder;
404 ParameterInfo[] pinfo = GetParameters ();
405 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
406 throw new ArgumentException ("parameters");
408 if (SecurityManager.SecurityEnabled) {
409 // sadly Attributes doesn't tell us which kind of security action this is so
410 // we must do it the hard way - and it also means that we can skip calling
411 // Attribute (which is another an icall)
412 SecurityManager.ReflectedLinkDemandInvoke (this);
415 Exception exc = null;
419 o = InternalInvoke (obj, parameters, out exc);
421 } catch (MethodAccessException) {
424 } catch (Exception e) {
425 throw new TargetInvocationException (e);
433 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
434 return Invoke (null, invokeAttr, binder, parameters, culture);
437 public override RuntimeMethodHandle MethodHandle {
438 get {return new RuntimeMethodHandle (mhandle);}
440 public override MethodAttributes Attributes {
443 MonoMethodInfo.get_method_info (mhandle, out info);
448 public override CallingConventions CallingConvention {
451 MonoMethodInfo.get_method_info (mhandle, out info);
452 return info.callconv;
456 public override Type ReflectedType {
461 public override Type DeclaringType {
464 MonoMethodInfo.get_method_info (mhandle, out info);
468 public override string Name {
472 return MonoMethod.get_name (this);
476 public override bool IsDefined (Type attributeType, bool inherit) {
477 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
480 public override object[] GetCustomAttributes( bool inherit) {
481 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
484 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
485 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
489 public override MethodBody GetMethodBody () {
490 return GetMethodBody (mhandle);
494 public override string ToString () {
495 StringBuilder sb = new StringBuilder ();
499 ParameterInfo[] p = GetParameters ();
500 for (int i = 0; i < p.Length; ++i) {
503 sb.Append (p[i].ParameterType.Name);
506 return sb.ToString ();
510 public void GetObjectData(SerializationInfo info, StreamingContext context)
512 MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);