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;
62 private IntPtr referenced_by;
65 private Delegate deleg;
66 private MonoMethod method;
67 private ParameterBuilder[] pinfo;
68 internal bool creating;
70 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
73 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
76 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
79 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
82 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, owner, owner.Module, skipVisibility, false) {
85 public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, attributes, callingConvention, returnType, parameterTypes, null, m, skipVisibility, false) {
88 public DynamicMethod (string name, Type returnType, Type[] parameterTypes) : this (name, returnType, parameterTypes, false) {
91 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, false, true) {
94 DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type [] parameterTypes, Type owner, Module m, bool skipVisibility, bool anonHosted)
97 throw new ArgumentNullException ("name");
98 if (returnType == null)
99 returnType = typeof (void);
100 if ((m == null) && !anonHosted)
101 throw new ArgumentNullException ("m");
102 if (returnType.IsByRef)
103 throw new ArgumentException ("Return type can't be a byref type", "returnType");
104 if (parameterTypes != null) {
105 for (int i = 0; i < parameterTypes.Length; ++i)
106 if (parameterTypes [i] == null)
107 throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
111 m = AnonHostModuleHolder.anon_host_module;
114 this.attributes = attributes | MethodAttributes.Static;
115 this.callingConvention = callingConvention;
116 this.returnType = returnType;
117 this.parameters = parameterTypes;
120 this.skipVisibility = skipVisibility;
123 [MethodImplAttribute(MethodImplOptions.InternalCall)]
124 private extern void create_dynamic_method (DynamicMethod m);
126 [MethodImplAttribute(MethodImplOptions.InternalCall)]
127 private extern void destroy_dynamic_method (DynamicMethod m);
129 private void CreateDynMethod () {
130 if (mhandle.Value == IntPtr.Zero) {
131 if (ilgen == null || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0))
132 throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
134 ilgen.label_fixup ();
136 // Have to create all DynamicMethods referenced by this one
138 // Used to avoid cycles
141 for (int i = 0; i < refs.Length; ++i) {
142 if (refs [i] is DynamicMethod) {
143 DynamicMethod m = (DynamicMethod)refs [i];
145 m.CreateDynMethod ();
153 create_dynamic_method (this);
159 destroy_dynamic_method (this);
163 public Delegate CreateDelegate (Type delegateType)
165 if (delegateType == null)
166 throw new ArgumentNullException ("delegateType");
172 deleg = Delegate.CreateDelegate (delegateType, this);
177 public Delegate CreateDelegate (Type delegateType, object target)
179 if (delegateType == null)
180 throw new ArgumentNullException ("delegateType");
184 /* Can't cache the delegate since it is different for each target */
185 return Delegate.CreateDelegate (delegateType, target, this);
188 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
191 // Extension: Mono allows position == 0 for the return attribute
193 if ((position < 0) || (position > parameters.Length))
194 throw new ArgumentOutOfRangeException ("position");
198 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
200 pinfo = new ParameterBuilder [parameters.Length + 1];
201 pinfo [position] = pb;
205 public override MethodInfo GetBaseDefinition () {
209 [MonoTODO("Not implemented")]
210 public override object[] GetCustomAttributes (bool inherit) {
211 throw new NotImplementedException ();
214 [MonoTODO("Not implemented")]
215 public override object[] GetCustomAttributes (Type attributeType,
217 throw new NotImplementedException ();
220 [MonoTODO("Not implemented")]
221 public DynamicILInfo GetDynamicILInfo () {
222 throw new NotImplementedException ();
225 public ILGenerator GetILGenerator () {
226 return GetILGenerator (64);
229 public ILGenerator GetILGenerator (int size) {
230 if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
231 MethodImplAttributes.IL) ||
232 ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
233 MethodImplAttributes.Managed))
234 throw new InvalidOperationException ("Method body should not exist.");
237 ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
241 public override MethodImplAttributes GetMethodImplementationFlags () {
242 return MethodImplAttributes.IL | MethodImplAttributes.Managed;
245 public override ParameterInfo[] GetParameters () {
246 if (parameters == null)
247 return new ParameterInfo [0];
249 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
250 for (int i = 0; i < parameters.Length; i++) {
251 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
257 public override object Invoke (object obj, object[] parameters) {
260 method = new MonoMethod (mhandle);
261 return method.Invoke (obj, parameters);
265 public override object Invoke (object obj, BindingFlags invokeAttr,
266 Binder binder, object[] parameters,
267 CultureInfo culture) {
270 method = new MonoMethod (mhandle);
271 return method.Invoke (obj, parameters);
274 [MonoTODO("Not implemented")]
275 public override bool IsDefined (Type attributeType, bool inherit) {
276 throw new NotImplementedException ();
279 public override string ToString () {
280 string parms = String.Empty;
281 ParameterInfo[] p = GetParameters ();
282 for (int i = 0; i < p.Length; ++i) {
284 parms = parms + ", ";
285 parms = parms + p [i].ParameterType.Name;
287 return ReturnType.Name+" "+Name+"("+parms+")";
290 public override MethodAttributes Attributes {
296 public override CallingConventions CallingConvention {
298 return callingConvention;
302 public override Type DeclaringType {
308 public bool InitLocals {
317 public override RuntimeMethodHandle MethodHandle {
323 public override Module Module {
329 public override string Name {
335 public override Type ReflectedType {
341 [MonoTODO("Not implemented")]
342 public override ParameterInfo ReturnParameter {
344 throw new NotImplementedException ();
348 public override Type ReturnType {
354 [MonoTODO("Not implemented")]
355 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
357 throw new NotImplementedException ();
362 public override int MetadataToken {
369 private void RejectIfCreated () {
370 if (mhandle.Value != IntPtr.Zero)
371 throw new InvalidOperationException ("Type definition of the method is complete.");
374 private Exception NotSupported () {
375 return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
378 internal int AddRef (object reference) {
380 refs = new object [4];
381 if (nrefs >= refs.Length - 1) {
382 object [] new_refs = new object [refs.Length * 2];
383 System.Array.Copy (refs, new_refs, refs.Length);
386 refs [nrefs] = reference;
387 /* Reserved by the runtime */
388 refs [nrefs + 1] = null;
393 // This class takes care of constructing the module in a thread safe manner
394 class AnonHostModuleHolder {
395 public static Module anon_host_module;
397 static AnonHostModuleHolder () {
398 AssemblyName aname = new AssemblyName ();
399 aname.Name = "Anonymously Hosted DynamicMethods Assembly";
400 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
402 anon_host_module = ab.GetManifestModule ();
407 internal class DynamicMethodTokenGenerator : TokenGenerator {
409 private DynamicMethod m;
411 public DynamicMethodTokenGenerator (DynamicMethod m) {
415 public int GetToken (string str) {
416 return m.AddRef (str);
419 public int GetToken (MethodInfo method, Type[] opt_param_types) {
420 throw new InvalidOperationException ();
423 public int GetToken (MemberInfo member) {
424 return m.AddRef (member);
427 public int GetToken (SignatureHelper helper) {
428 return m.AddRef (helper);