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;
38 namespace System.Reflection {
40 internal struct MonoMethodInfo
44 internal MethodAttributes attrs;
45 internal MethodImplAttributes iattrs;
46 internal CallingConventions callconv;
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
51 [MethodImplAttribute(MethodImplOptions.InternalCall)]
52 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 internal static extern UnmanagedMarshal get_retval_marshal (IntPtr handle);
59 * Note: most of this class needs to be duplicated for the contructor, since
60 * the .NET reflection class hierarchy is so broken.
63 internal class MonoMethod : MethodInfo, ISerializable
65 internal IntPtr mhandle;
69 internal MonoMethod () {
72 internal MonoMethod (RuntimeMethodHandle mhandle) {
73 this.mhandle = mhandle.Value;
76 [MethodImplAttribute(MethodImplOptions.InternalCall)]
77 internal static extern MonoMethod get_base_definition (MonoMethod method);
79 public override MethodInfo GetBaseDefinition ()
81 return get_base_definition (this);
84 public override Type ReturnType {
87 MonoMethodInfo.get_method_info (mhandle, out info);
91 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
93 return new ParameterInfo (ReturnType, this, MonoMethodInfo.get_retval_marshal (mhandle));
97 public override MethodImplAttributes GetMethodImplementationFlags() {
99 MonoMethodInfo.get_method_info (mhandle, out info);
103 public override ParameterInfo[] GetParameters() {
104 return MonoMethodInfo.get_parameter_info (mhandle);
108 * InternalInvoke() receives the parameters correctly converted by the
109 * binder to match the types of the method signature.
111 [MethodImplAttribute(MethodImplOptions.InternalCall)]
112 internal extern Object InternalInvoke (Object obj, Object[] parameters);
114 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
117 binder = Binder.DefaultBinder;
118 ParameterInfo[] pinfo = GetParameters ();
119 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
120 throw new ArgumentException ("parameters");
122 if (SecurityManager.SecurityEnabled) {
123 // sadly Attributes doesn't tell us which kind of security action this is so
124 // we must do it the hard way - and it also means that we can skip calling
125 // Attribute (which is another an icall)
126 SecurityManager.ReflectedLinkDemandInvoke (this);
130 if (ContainsGenericParameters)
131 throw new InvalidOperationException ("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.");
135 return InternalInvoke (obj, parameters);
136 } catch (InvalidOperationException) {
138 } catch (TargetException) {
140 } catch (Exception e) {
141 throw new TargetInvocationException (e);
145 public override RuntimeMethodHandle MethodHandle {
146 get {return new RuntimeMethodHandle (mhandle);}
148 public override MethodAttributes Attributes {
151 MonoMethodInfo.get_method_info (mhandle, out info);
156 public override CallingConventions CallingConvention {
159 MonoMethodInfo.get_method_info (mhandle, out info);
160 return info.callconv;
164 public override Type ReflectedType {
169 public override Type DeclaringType {
172 MonoMethodInfo.get_method_info (mhandle, out info);
176 public override string Name {
182 public override bool IsDefined (Type attributeType, bool inherit) {
183 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
186 public override object[] GetCustomAttributes( bool inherit) {
187 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
189 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
190 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
193 [MethodImplAttribute(MethodImplOptions.InternalCall)]
194 internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
196 internal object[] GetPseudoCustomAttributes ()
200 /* MS.NET doesn't report MethodImplAttribute */
203 MonoMethodInfo.get_method_info (mhandle, out info);
204 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
206 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
211 object[] attrs = new object [count];
214 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
215 attrs [count ++] = new PreserveSigAttribute ();
216 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
217 DllImportAttribute attr = GetDllImportAttribute (mhandle);
218 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
219 attr.PreserveSig = true;
220 attrs [count ++] = attr;
226 public override string ToString () {
228 ParameterInfo[] p = GetParameters ();
229 for (int i = 0; i < p.Length; ++i) {
231 parms = parms + ", ";
232 Type pt = p[i].ParameterType;
233 if (pt.IsClass && pt.Namespace != "")
234 parms = parms + pt.Namespace + "." + pt.Name;
236 parms = parms + pt.Name;
240 if (ReturnType.IsClass && ReturnType.Namespace != "")
241 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
243 #if NET_2_0 || BOOTSTRAP_NET_2_0
244 if (IsGenericMethod) {
245 Type[] gen_params = GetGenericArguments ();
247 for (int j = 0; j < gen_params.Length; j++) {
250 generic += gen_params [j].Name;
255 return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
260 public void GetObjectData(SerializationInfo info, StreamingContext context)
262 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
265 #if NET_2_0 || BOOTSTRAP_NET_2_0
266 public override MethodInfo MakeGenericMethod (Type [] types)
269 throw new ArgumentNullException ("types");
270 MethodInfo ret = MakeGenericMethod_impl (types);
272 throw new ArgumentException (String.Format ("The method has {0} generic parameter(s) but {1} generic argument(s) were provided.", GetGenericArguments ().Length, types.Length));
276 [MethodImplAttribute(MethodImplOptions.InternalCall)]
277 extern MethodInfo MakeGenericMethod_impl (Type [] types);
279 [MethodImplAttribute(MethodImplOptions.InternalCall)]
280 public override extern Type [] GetGenericArguments ();
282 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283 extern MethodInfo GetGenericMethodDefinition_impl ();
285 public override MethodInfo GetGenericMethodDefinition ()
287 MethodInfo res = GetGenericMethodDefinition_impl ();
289 throw new InvalidOperationException ();
294 public override extern bool IsGenericMethodDefinition {
295 [MethodImplAttribute(MethodImplOptions.InternalCall)]
299 public override extern bool IsGenericMethod {
300 [MethodImplAttribute(MethodImplOptions.InternalCall)]
304 public override bool ContainsGenericParameters {
306 if (IsGenericMethod) {
307 foreach (Type arg in GetGenericArguments ())
308 if (arg.ContainsGenericParameters)
311 return DeclaringType.ContainsGenericParameters;
317 public override MethodBody GetMethodBody () {
318 return GetMethodBody (mhandle);
323 internal class MonoCMethod : ConstructorInfo, ISerializable
325 internal IntPtr mhandle;
329 public override MethodImplAttributes GetMethodImplementationFlags() {
331 MonoMethodInfo.get_method_info (mhandle, out info);
335 public override ParameterInfo[] GetParameters() {
336 return MonoMethodInfo.get_parameter_info (mhandle);
340 * InternalInvoke() receives the parameters corretcly converted by the binder
341 * to match the types of the method signature.
343 [MethodImplAttribute(MethodImplOptions.InternalCall)]
344 internal extern Object InternalInvoke (Object obj, Object[] parameters);
346 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
349 binder = Binder.DefaultBinder;
350 ParameterInfo[] pinfo = GetParameters ();
351 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
352 throw new ArgumentException ("parameters");
354 if (SecurityManager.SecurityEnabled) {
355 // sadly Attributes doesn't tell us which kind of security action this is so
356 // we must do it the hard way - and it also means that we can skip calling
357 // Attribute (which is another an icall)
358 SecurityManager.ReflectedLinkDemandInvoke (this);
362 return InternalInvoke (obj, parameters);
363 } catch (InvalidOperationException) {
365 } catch (TargetException) {
367 } catch (Exception e) {
368 throw new TargetInvocationException (e);
372 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
373 return Invoke (null, invokeAttr, binder, parameters, culture);
376 public override RuntimeMethodHandle MethodHandle {
377 get {return new RuntimeMethodHandle (mhandle);}
379 public override MethodAttributes Attributes {
382 MonoMethodInfo.get_method_info (mhandle, out info);
387 public override CallingConventions CallingConvention {
390 MonoMethodInfo.get_method_info (mhandle, out info);
391 return info.callconv;
395 public override Type ReflectedType {
400 public override Type DeclaringType {
403 MonoMethodInfo.get_method_info (mhandle, out info);
407 public override string Name {
413 public override bool IsDefined (Type attributeType, bool inherit) {
414 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
417 public override object[] GetCustomAttributes( bool inherit) {
418 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
421 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
422 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
425 #if NET_2_0 || BOOTSTRAP_NET_2_0
426 public override bool IsGenericMethodDefinition {
432 public override bool IsGenericMethod {
440 public override MethodBody GetMethodBody () {
441 return GetMethodBody (mhandle);
445 public override string ToString () {
447 ParameterInfo[] p = GetParameters ();
448 for (int i = 0; i < p.Length; ++i) {
450 parms = parms + ", ";
451 parms = parms + p [i].ParameterType.Name;
453 return "Void "+Name+"("+parms+")";
457 public void GetObjectData(SerializationInfo info, StreamingContext context)
459 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);