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;
229 if (ReturnType.IsClass && ReturnType.Namespace != "")
230 return ReturnType.Namespace + "." + ReturnType.Name + " " + Name + "(" + parms + ")";
232 #if NET_2_0 || BOOTSTRAP_NET_2_0
233 if (HasGenericParameters) {
234 Type[] gen_params = GetGenericArguments ();
236 for (int j = 0; j < gen_params.Length; j++) {
239 generic += gen_params [j].Name;
244 return ReturnType.Name + " " + Name + generic + "(" + parms + ")";
249 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
251 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method);
\r
254 #if NET_2_0 || BOOTSTRAP_NET_2_0
255 [MethodImplAttribute(MethodImplOptions.InternalCall)]
256 public override extern MethodInfo MakeGenericMethod (Type [] types);
258 [MethodImplAttribute(MethodImplOptions.InternalCall)]
259 public override extern Type [] GetGenericArguments ();
261 [MethodImplAttribute(MethodImplOptions.InternalCall)]
262 extern MethodInfo GetGenericMethodDefinition_impl ();
264 public override MethodInfo GetGenericMethodDefinition ()
266 MethodInfo res = GetGenericMethodDefinition_impl ();
268 throw new InvalidOperationException ();
273 public override extern bool Mono_IsInflatedMethod {
274 [MethodImplAttribute(MethodImplOptions.InternalCall)]
278 public override extern bool HasGenericParameters {
279 [MethodImplAttribute(MethodImplOptions.InternalCall)]
283 public override extern bool IsGenericMethodDefinition {
284 [MethodImplAttribute(MethodImplOptions.InternalCall)]
290 public override MethodBody GetMethodBody () {
291 return GetMethodBody (mhandle);
296 internal class MonoCMethod : ConstructorInfo, ISerializable
\r
298 internal IntPtr mhandle;
302 public override MethodImplAttributes GetMethodImplementationFlags() {
304 MonoMethodInfo.get_method_info (mhandle, out info);
308 public override ParameterInfo[] GetParameters() {
309 return MonoMethodInfo.get_parameter_info (mhandle);
313 * InternalInvoke() receives the parameters corretcly converted by the binder
314 * to match the types of the method signature.
316 [MethodImplAttribute(MethodImplOptions.InternalCall)]
317 internal extern Object InternalInvoke (Object obj, Object[] parameters);
319 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
322 binder = Binder.DefaultBinder;
323 ParameterInfo[] pinfo = GetParameters ();
324 if (!Binder.ConvertArgs (binder, parameters, pinfo, culture))
325 throw new ArgumentException ("parameters");
327 if (SecurityManager.SecurityEnabled) {
328 // sadly Attributes doesn't tell us which kind of security action this is so
329 // we must do it the hard way - and it also means that we can skip calling
330 // Attribute (which is another an icall)
331 SecurityManager.ReflectedLinkDemandInvoke (this);
335 return InternalInvoke (obj, parameters);
336 } catch (InvalidOperationException) {
338 } catch (TargetException) {
340 } catch (Exception e) {
341 throw new TargetInvocationException (e);
345 public override Object Invoke (BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
346 return Invoke (null, invokeAttr, binder, parameters, culture);
349 public override RuntimeMethodHandle MethodHandle {
350 get {return new RuntimeMethodHandle (mhandle);}
352 public override MethodAttributes Attributes {
355 MonoMethodInfo.get_method_info (mhandle, out info);
360 public override CallingConventions CallingConvention {
363 MonoMethodInfo.get_method_info (mhandle, out info);
364 return info.callconv;
368 public override Type ReflectedType {
373 public override Type DeclaringType {
376 MonoMethodInfo.get_method_info (mhandle, out info);
380 public override string Name {
386 public override bool IsDefined (Type attributeType, bool inherit) {
387 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
390 public override object[] GetCustomAttributes( bool inherit) {
391 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
394 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
395 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
398 #if NET_2_0 || BOOTSTRAP_NET_2_0
399 [MethodImplAttribute(MethodImplOptions.InternalCall)]
400 extern MethodInfo GetGenericMethodDefinition_impl ();
402 public override MethodInfo GetGenericMethodDefinition ()
404 MethodInfo res = GetGenericMethodDefinition_impl ();
406 throw new InvalidOperationException ();
411 public override extern bool Mono_IsInflatedMethod {
412 [MethodImplAttribute(MethodImplOptions.InternalCall)]
416 public override bool HasGenericParameters {
422 public override bool IsGenericMethodDefinition {
430 public override MethodBody GetMethodBody () {
431 return GetMethodBody (mhandle);
435 public override string ToString () {
437 ParameterInfo[] p = GetParameters ();
438 for (int i = 0; i < p.Length; ++i) {
440 parms = parms + ", ";
441 parms = parms + p [i].ParameterType.Name;
443 return "Void "+Name+"("+parms+")";
447 public void GetObjectData(SerializationInfo info, StreamingContext context)
\r
449 ReflectionSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
\r