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.Security;
37 namespace System.Reflection {
39 internal struct MonoMethodInfo
43 internal MethodAttributes attrs;
44 internal MethodImplAttributes iattrs;
45 internal CallingConventions callconv;
47 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48 internal static extern void get_method_info (IntPtr handle, out MonoMethodInfo info);
50 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51 internal static extern ParameterInfo[] get_parameter_info (IntPtr handle);
55 * Note: most of this class needs to be duplicated for the contructor, since
56 * the .NET reflection class hierarchy is so broken.
59 internal class MonoMethod : MethodInfo, ISerializable
\r
61 internal IntPtr mhandle;
65 internal MonoMethod () {
68 internal MonoMethod (RuntimeMethodHandle mhandle) {
69 this.mhandle = mhandle.Value;
72 [MethodImplAttribute(MethodImplOptions.InternalCall)]
73 internal static extern MonoMethod get_base_definition (MonoMethod method);
75 public override MethodInfo GetBaseDefinition ()
77 return get_base_definition (this);
80 public override Type ReturnType {
83 MonoMethodInfo.get_method_info (mhandle, out info);
87 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
89 return new ParameterInfo (ReturnType, this);
93 public override MethodImplAttributes GetMethodImplementationFlags() {
95 MonoMethodInfo.get_method_info (mhandle, out info);
99 public override ParameterInfo[] GetParameters() {
100 return MonoMethodInfo.get_parameter_info (mhandle);
104 * InternalInvoke() receives the parameters correctly converted by the
105 * binder to match the types of the method signature.
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 internal extern Object InternalInvoke (Object obj, Object[] parameters);
110 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
113 binder = Binder.DefaultBinder;
114 ParameterInfo[] pinfo = GetParameters ();
115 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
116 throw new ArgumentException ("parameters");
118 if (SecurityManager.SecurityEnabled) {
119 // sadly Attributes doesn't tell us which kind of security action this is so
120 // we must do it the hard way - and it also means that we can skip calling
121 // Attribute (which is another an icall)
122 SecurityManager.ReflectedLinkDemandInvoke (this);
126 return InternalInvoke (obj, parameters);
127 } catch (InvalidOperationException) {
129 } catch (TargetException) {
131 } catch (Exception e) {
132 throw new TargetInvocationException (e);
136 public override RuntimeMethodHandle MethodHandle {
137 get {return new RuntimeMethodHandle (mhandle);}
139 public override MethodAttributes Attributes {
142 MonoMethodInfo.get_method_info (mhandle, out info);
147 public override CallingConventions CallingConvention {
150 MonoMethodInfo.get_method_info (mhandle, out info);
151 return info.callconv;
155 public override Type ReflectedType {
160 public override Type DeclaringType {
163 MonoMethodInfo.get_method_info (mhandle, out info);
167 public override string Name {
173 public override bool IsDefined (Type attributeType, bool inherit) {
174 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
177 public override object[] GetCustomAttributes( bool inherit) {
178 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
180 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
181 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
184 [MethodImplAttribute(MethodImplOptions.InternalCall)]
185 internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
187 internal object[] GetPseudoCustomAttributes ()
191 /* MS.NET doesn't report MethodImplAttribute */
194 MonoMethodInfo.get_method_info (mhandle, out info);
195 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
197 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0)
202 object[] attrs = new object [count];
205 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
206 attrs [count ++] = new PreserveSigAttribute ();
207 if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
208 DllImportAttribute attr = GetDllImportAttribute (mhandle);
209 if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
210 attr.PreserveSig = true;
211 attrs [count ++] = attr;
217 public override string ToString () {
219 ParameterInfo[] p = GetParameters ();
220 for (int i = 0; i < p.Length; ++i) {
222 parms = parms + ", ";
223 Type pt = p[i].ParameterType;
224 if (pt.IsClass && pt.Namespace != "")
225 parms = parms + pt.Namespace + "." + pt.Name;
227 parms = parms + pt.Name;
231 if (ReturnType.IsClass && ReturnType.Namespace != "")
232 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
234 #if NET_2_0 || BOOTSTRAP_NET_2_0
235 if (HasGenericParameters) {
236 Type[] gen_params = GetGenericArguments ();
238 for (int j = 0; j < gen_params.Length; j++) {
241 generic += gen_params [j].Name;
246 return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
251 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
253 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
\r
256 #if NET_2_0 || BOOTSTRAP_NET_2_0
257 public override MethodInfo MakeGenericMethod (Type [] types)
260 throw new ArgumentNullException ("types");
261 MethodInfo ret = MakeGenericMethod_impl (types);
263 throw new ArgumentException (String.Format ("The method has {0} generic parameter(s) but {1} generic argument(s) were provided.", GetGenericArguments ().Length, types.Length));
267 [MethodImplAttribute(MethodImplOptions.InternalCall)]
268 extern MethodInfo MakeGenericMethod_impl (Type [] types);
270 [MethodImplAttribute(MethodImplOptions.InternalCall)]
271 public override extern Type [] GetGenericArguments ();
273 [MethodImplAttribute(MethodImplOptions.InternalCall)]
274 extern MethodInfo GetGenericMethodDefinition_impl ();
276 public override MethodInfo GetGenericMethodDefinition ()
278 MethodInfo res = GetGenericMethodDefinition_impl ();
280 throw new InvalidOperationException ();
285 public override extern bool Mono_IsInflatedMethod {
286 [MethodImplAttribute(MethodImplOptions.InternalCall)]
290 public override extern bool HasGenericParameters {
291 [MethodImplAttribute(MethodImplOptions.InternalCall)]
295 public override extern bool IsGenericMethodDefinition {
296 [MethodImplAttribute(MethodImplOptions.InternalCall)]
302 public override MethodBody GetMethodBody () {
303 return GetMethodBody (mhandle);
308 internal class MonoCMethod : ConstructorInfo, ISerializable
\r
310 internal IntPtr mhandle;
314 public override MethodImplAttributes GetMethodImplementationFlags() {
316 MonoMethodInfo.get_method_info (mhandle, out info);
320 public override ParameterInfo[] GetParameters() {
321 return MonoMethodInfo.get_parameter_info (mhandle);
325 * InternalInvoke() receives the parameters corretcly converted by the binder
326 * to match the types of the method signature.
328 [MethodImplAttribute(MethodImplOptions.InternalCall)]
329 internal extern Object InternalInvoke (Object obj, Object[] parameters);
331 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
334 binder = Binder.DefaultBinder;
335 ParameterInfo[] pinfo = GetParameters ();
336 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
337 throw new ArgumentException ("parameters");
339 if (SecurityManager.SecurityEnabled) {
340 // sadly Attributes doesn't tell us which kind of security action this is so
341 // we must do it the hard way - and it also means that we can skip calling
342 // Attribute (which is another an icall)
343 SecurityManager.ReflectedLinkDemandInvoke (this);
347 return InternalInvoke (obj, parameters);
348 } catch (InvalidOperationException) {
350 } catch (TargetException) {
352 } catch (Exception e) {
353 throw new TargetInvocationException (e);
357 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
358 return Invoke (null, invokeAttr, binder, parameters, culture);
361 public override RuntimeMethodHandle MethodHandle {
362 get {return new RuntimeMethodHandle (mhandle);}
364 public override MethodAttributes Attributes {
367 MonoMethodInfo.get_method_info (mhandle, out info);
372 public override CallingConventions CallingConvention {
375 MonoMethodInfo.get_method_info (mhandle, out info);
376 return info.callconv;
380 public override Type ReflectedType {
385 public override Type DeclaringType {
388 MonoMethodInfo.get_method_info (mhandle, out info);
392 public override string Name {
398 public override bool IsDefined (Type attributeType, bool inherit) {
399 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
402 public override object[] GetCustomAttributes( bool inherit) {
403 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
406 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
407 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
410 #if NET_2_0 || BOOTSTRAP_NET_2_0
411 [MethodImplAttribute(MethodImplOptions.InternalCall)]
412 extern MethodInfo GetGenericMethodDefinition_impl ();
414 public override MethodInfo GetGenericMethodDefinition ()
416 MethodInfo res = GetGenericMethodDefinition_impl ();
418 throw new InvalidOperationException ();
423 public override extern bool Mono_IsInflatedMethod {
424 [MethodImplAttribute(MethodImplOptions.InternalCall)]
428 public override bool HasGenericParameters {
434 public override bool IsGenericMethodDefinition {
442 public override MethodBody GetMethodBody () {
443 return GetMethodBody (mhandle);
447 public override string ToString () {
449 ParameterInfo[] p = GetParameters ();
450 for (int i = 0; i < p.Length; ++i) {
452 parms = parms + ", ";
453 parms = parms + p [i].ParameterType.Name;
455 return "Void "+Name+"("+parms+")";
459 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
461 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
\r