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.
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
42 namespace System.Reflection.Emit {
45 public sealed class DynamicMethod : MethodInfo {
47 #pragma warning disable 169, 414
48 #region Sync with reflection.h
49 private RuntimeMethodHandle mhandle;
51 private Type returnType;
52 private Type[] parameters;
53 private MethodAttributes attributes;
54 private CallingConventions callingConvention;
55 private Module module;
56 private bool skipVisibility;
57 private bool init_locals = true;
58 private ILGenerator ilgen;
60 private object[] refs;
61 private IntPtr referenced_by;
64 #pragma warning restore 169, 414
66 private Delegate deleg;
67 private MonoMethod method;
68 private ParameterBuilder[] pinfo;
69 internal bool creating;
70 private DynamicILInfo il_info;
72 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
75 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
78 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
81 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
84 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) {
87 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) {
90 public DynamicMethod (string name, Type returnType, Type[] parameterTypes) : this (name, returnType, parameterTypes, false) {
93 [MonoTODO ("Visibility is not restricted")]
94 public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
95 : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, restrictedSkipVisibility, true)
99 DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type [] parameterTypes, Type owner, Module m, bool skipVisibility, bool anonHosted)
102 throw new ArgumentNullException ("name");
103 if (returnType == null)
104 returnType = typeof (void);
105 if ((m == null) && !anonHosted)
106 throw new ArgumentNullException ("m");
107 if (returnType.IsByRef)
108 throw new ArgumentException ("Return type can't be a byref type", "returnType");
109 if (parameterTypes != null) {
110 for (int i = 0; i < parameterTypes.Length; ++i)
111 if (parameterTypes [i] == null)
112 throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
116 m = AnonHostModuleHolder.anon_host_module;
119 this.attributes = attributes | MethodAttributes.Static;
120 this.callingConvention = callingConvention;
121 this.returnType = returnType;
122 this.parameters = parameterTypes;
125 this.skipVisibility = skipVisibility;
128 [MethodImplAttribute(MethodImplOptions.InternalCall)]
129 private extern void create_dynamic_method (DynamicMethod m);
131 private void CreateDynMethod () {
132 if (mhandle.Value == IntPtr.Zero) {
133 if (ilgen == null || ilgen.ILOffset == 0)
134 throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
136 ilgen.label_fixup ();
138 // Have to create all DynamicMethods referenced by this one
140 // Used to avoid cycles
143 for (int i = 0; i < refs.Length; ++i) {
144 if (refs [i] is DynamicMethod) {
145 DynamicMethod m = (DynamicMethod)refs [i];
147 m.CreateDynMethod ();
155 create_dynamic_method (this);
160 public Delegate CreateDelegate (Type delegateType)
162 if (delegateType == null)
163 throw new ArgumentNullException ("delegateType");
169 deleg = Delegate.CreateDelegate (delegateType, this);
174 public Delegate CreateDelegate (Type delegateType, object target)
176 if (delegateType == null)
177 throw new ArgumentNullException ("delegateType");
181 /* Can't cache the delegate since it is different for each target */
182 return Delegate.CreateDelegate (delegateType, target, this);
185 public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string parameterName)
188 // Extension: Mono allows position == 0 for the return attribute
190 if ((position < 0) || (position > parameters.Length))
191 throw new ArgumentOutOfRangeException ("position");
195 ParameterBuilder pb = new ParameterBuilder (this, position, attributes, parameterName);
197 pinfo = new ParameterBuilder [parameters.Length + 1];
198 pinfo [position] = pb;
202 public override MethodInfo GetBaseDefinition () {
206 [MonoTODO("Not implemented")]
207 public override object[] GetCustomAttributes (bool inherit) {
208 throw new NotImplementedException ();
211 [MonoTODO("Not implemented")]
212 public override object[] GetCustomAttributes (Type attributeType,
214 throw new NotImplementedException ();
217 public DynamicILInfo GetDynamicILInfo () {
219 il_info = new DynamicILInfo (this);
223 public ILGenerator GetILGenerator () {
224 return GetILGenerator (64);
227 public ILGenerator GetILGenerator (int streamSize) {
228 if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
229 MethodImplAttributes.IL) ||
230 ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
231 MethodImplAttributes.Managed))
232 throw new InvalidOperationException ("Method body should not exist.");
235 ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), streamSize);
239 public override MethodImplAttributes GetMethodImplementationFlags () {
240 return MethodImplAttributes.IL | MethodImplAttributes.Managed;
243 public override ParameterInfo[] GetParameters () {
244 if (parameters == null)
245 return new ParameterInfo [0];
247 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
248 for (int i = 0; i < parameters.Length; i++) {
249 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
254 internal override int GetParameterCount ()
256 return parameters == null ? 0 : parameters.Length;
260 public override object Invoke (object obj, object[] parameters) {
263 method = new MonoMethod (mhandle);
264 return method.Invoke (obj, parameters);
268 public override object Invoke (object obj, BindingFlags invokeAttr,
269 Binder binder, object[] parameters,
275 method = new MonoMethod (mhandle);
277 return method.Invoke (obj, parameters);
279 catch (MethodAccessException mae) {
280 throw new TargetInvocationException ("Method cannot be invoked.", mae);
284 [MonoTODO("Not implemented")]
285 public override bool IsDefined (Type attributeType, bool inherit) {
286 throw new NotImplementedException ();
289 public override string ToString () {
290 string parms = String.Empty;
291 ParameterInfo[] p = GetParameters ();
292 for (int i = 0; i < p.Length; ++i) {
294 parms = parms + ", ";
295 parms = parms + p [i].ParameterType.Name;
297 return ReturnType.Name+" "+Name+"("+parms+")";
300 public override MethodAttributes Attributes {
306 public override CallingConventions CallingConvention {
308 return callingConvention;
312 public override Type DeclaringType {
318 public bool InitLocals {
327 public override RuntimeMethodHandle MethodHandle {
333 public override Module Module {
339 public override string Name {
345 public override Type ReflectedType {
351 [MonoTODO("Not implemented")]
352 public override ParameterInfo ReturnParameter {
354 throw new NotImplementedException ();
358 public override Type ReturnType {
364 [MonoTODO("Not implemented")]
365 public override ICustomAttributeProvider ReturnTypeCustomAttributes {
367 throw new NotImplementedException ();
372 public override int MetadataToken {
379 private void RejectIfCreated () {
380 if (mhandle.Value != IntPtr.Zero)
381 throw new InvalidOperationException ("Type definition of the method is complete.");
384 internal int AddRef (object reference) {
386 refs = new object [4];
387 if (nrefs >= refs.Length - 1) {
388 object [] new_refs = new object [refs.Length * 2];
389 System.Array.Copy (refs, new_refs, refs.Length);
392 refs [nrefs] = reference;
393 /* Reserved by the runtime */
394 refs [nrefs + 1] = null;
399 // This class takes care of constructing the module in a thread safe manner
400 class AnonHostModuleHolder {
401 public static Module anon_host_module;
403 static AnonHostModuleHolder () {
404 AssemblyName aname = new AssemblyName ();
405 aname.Name = "Anonymously Hosted DynamicMethods Assembly";
406 AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Run);
408 anon_host_module = ab.GetManifestModule ();
413 internal class DynamicMethodTokenGenerator : TokenGenerator {
415 private DynamicMethod m;
417 public DynamicMethodTokenGenerator (DynamicMethod m) {
421 public int GetToken (string str) {
422 return m.AddRef (str);
425 public int GetToken (MethodInfo method, Type[] opt_param_types) {
426 throw new InvalidOperationException ();
429 public int GetToken (MemberInfo member, bool create_open_instance) {
430 return m.AddRef (member);
433 public int GetToken (SignatureHelper helper) {
434 return m.AddRef (helper);