2 // System.Reflection.Emit/DynamicMethod.cs
5 // Paolo Molaro (lupus@ximian.com)
6 // Zoltan Varga (vargaz@freemail.hu)
8 // (C) 2003 Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #if NET_2_0 || BOOTSTRAP_NET_2_0
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Globalization;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
43 namespace System.Reflection.Emit {
48 public sealed class DynamicMethod : MethodInfo {
49 #region Sync with reflection.h
50 private RuntimeMethodHandle mhandle;
52 private Type returnType;
53 private Type[] parameters;
54 private MethodAttributes attributes;
55 private CallingConventions callingConvention;
56 private Module module;
57 private bool skipVisibility;
58 private bool init_locals = true;
59 private ILGenerator ilgen;
61 private object[] refs;
63 private Delegate deleg;
64 private MonoMethod method;
65 private ParameterBuilder[] pinfo;
66 internal bool creating;
68 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
71 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
74 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
77 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
80 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, owner.Module, skipVisibility) {
83 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) {
85 throw new ArgumentNullException ("name");
86 if (returnType == null)
87 returnType = typeof (void);
89 throw new ArgumentNullException ("m");
90 if (returnType.IsByRef)
91 throw new ArgumentException ("Return type can't be a byref type", "returnType");
92 if (parameterTypes != null) {
93 for (int i = 0; i < parameterTypes.Length; ++i)
94 if (parameterTypes [i] == null)
95 throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
99 this.attributes = attributes | MethodAttributes.Static;
100 this.callingConvention = callingConvention;
101 this.returnType = returnType;
102 this.parameters = parameterTypes;
104 this.skipVisibility = skipVisibility;
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 private extern void create_dynamic_method (DynamicMethod m);
110 private void CreateDynMethod () {
111 if (mhandle.Value == IntPtr.Zero) {
112 if (ilgen == null || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0))
113 throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
115 ilgen.label_fixup ();
117 // Have to create all DynamicMethods referenced by this one
119 // Used to avoid cycles
122 for (int i = 0; i < refs.Length; ++i) {
123 if (refs [i] is DynamicMethod) {
124 DynamicMethod m = (DynamicMethod)refs [i];
126 m.CreateDynMethod ();
134 create_dynamic_method (this);
139 public Delegate CreateDelegate (Type delegateType)
141 if (delegateType == null)
142 throw new ArgumentNullException ("delegateType");
148 deleg = Delegate.CreateDelegate (delegateType, this);
153 public Delegate CreateDelegate (Type delegateType, object target)
155 if (delegateType == null)
156 throw new ArgumentNullException ("delegateType");
160 /* Can't cache the delegate since it is different for each target */
161 return Delegate.CreateDelegate (delegateType, target, this);
164 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
167 // Extension: Mono allows position == 0 for the return attribute
169 if ((position < 0) || (position > parameters.Length))
170 throw new ArgumentOutOfRangeException ("position");
174 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
176 pinfo = new ParameterBuilder [parameters.Length + 1];
177 pinfo [position] = pb;
181 public override MethodInfo GetBaseDefinition () {
186 public override object[] GetCustomAttributes (bool inherit) {
187 throw new NotImplementedException ();
191 public override object[] GetCustomAttributes (Type attributeType,
193 throw new NotImplementedException ();
196 public ILGenerator GetILGenerator () {
197 return GetILGenerator (64);
200 public ILGenerator GetILGenerator (int size) {
201 if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
202 MethodImplAttributes.IL) ||
203 ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
204 MethodImplAttributes.Managed))
205 throw new InvalidOperationException ("Method body should not exist.");
208 ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
212 public override MethodImplAttributes GetMethodImplementationFlags () {
213 return MethodImplAttributes.IL | MethodImplAttributes.Managed;
216 public override ParameterInfo[] GetParameters () {
217 if (parameters == null)
218 return new ParameterInfo [0];
220 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
221 for (int i = 0; i < parameters.Length; i++) {
222 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
227 public override object Invoke (object obj, object[] parameters) {
230 method = new MonoMethod (mhandle);
231 return method.Invoke (obj, parameters);
234 public override object Invoke (object obj, BindingFlags invokeAttr,
235 Binder binder, object[] parameters,
236 CultureInfo culture) {
239 method = new MonoMethod (mhandle);
240 return method.Invoke (obj, parameters);
244 public override bool IsDefined (Type attributeType, bool inherit) {
245 throw new NotImplementedException ();
248 public override string ToString () {
249 string parms = String.Empty;
250 ParameterInfo[] p = GetParameters ();
251 for (int i = 0; i < p.Length; ++i) {
253 parms = parms + ", ";
254 parms = parms + p [i].ParameterType.Name;
256 return ReturnType.Name+" "+Name+"("+parms+")";
259 public override MethodAttributes Attributes {
265 public override CallingConventions CallingConvention {
267 return callingConvention;
271 public override Type DeclaringType {
277 public bool InitLocals {
286 public override RuntimeMethodHandle MethodHandle {
292 public override Module Module {
298 public override string Name {
304 public override Type ReflectedType {
311 public override ParameterInfo ReturnParameter {
313 throw new NotImplementedException ();
317 public override Type ReturnType {
324 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
326 throw new NotImplementedException ();
330 public override int MetadataToken {
336 private void RejectIfCreated () {
337 if (mhandle.Value != IntPtr.Zero)
338 throw new InvalidOperationException ("Type definition of the method is complete.");
341 private Exception NotSupported () {
342 return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
345 internal int AddRef (object reference) {
347 refs = new object [4];
348 if (nrefs >= refs.Length - 1) {
349 object [] new_refs = new object [refs.Length * 2];
350 System.Array.Copy (refs, new_refs, refs.Length);
353 refs [nrefs] = reference;
354 /* Reserved by the runtime */
355 refs [nrefs + 1] = null;
361 internal class DynamicMethodTokenGenerator : TokenGenerator {
363 private DynamicMethod m;
365 public DynamicMethodTokenGenerator (DynamicMethod m) {
369 public int GetToken (string str) {
370 return m.AddRef (str);
373 public int GetToken (MethodInfo method, Type[] opt_param_types) {
374 throw new InvalidOperationException ();
377 public int GetToken (MemberInfo member) {
378 return m.AddRef (member);
381 public int GetToken (SignatureHelper helper) {
382 return m.AddRef (helper);