DynamicMethod.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // System.Reflection.Emit/DynamicMethod.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. // Zoltan Varga ([email protected])
  7. //
  8. // (C) 2003 Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0 || BOOTSTRAP_NET_2_0
  33. using System;
  34. using System.Reflection;
  35. using System.Reflection.Emit;
  36. using System.Globalization;
  37. using System.Runtime.CompilerServices;
  38. using System.Runtime.InteropServices;
  39. namespace System.Reflection.Emit {
  40. #if NET_2_0
  41. [ComVisible (true)]
  42. #endif
  43. public sealed class DynamicMethod : MethodInfo {
  44. #region Sync with reflection.h
  45. private RuntimeMethodHandle mhandle;
  46. private string name;
  47. private Type returnType;
  48. private Type[] parameters;
  49. private MethodAttributes attributes;
  50. private CallingConventions callingConvention;
  51. private Module module;
  52. private bool skipVisibility;
  53. private bool init_locals = true;
  54. private ILGenerator ilgen;
  55. private int nrefs;
  56. private object[] refs;
  57. private IntPtr referenced_by;
  58. private Type owner;
  59. #endregion
  60. private Delegate deleg;
  61. private MonoMethod method;
  62. private ParameterBuilder[] pinfo;
  63. internal bool creating;
  64. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
  65. }
  66. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
  67. }
  68. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
  69. }
  70. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
  71. }
  72. 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) {
  73. }
  74. 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) {
  75. }
  76. DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type [] parameterTypes, Type owner, Module m, bool skipVisibility)
  77. {
  78. if (name == null)
  79. throw new ArgumentNullException ("name");
  80. if (returnType == null)
  81. returnType = typeof (void);
  82. if (m == null)
  83. throw new ArgumentNullException ("m");
  84. if (returnType.IsByRef)
  85. throw new ArgumentException ("Return type can't be a byref type", "returnType");
  86. if (parameterTypes != null) {
  87. for (int i = 0; i < parameterTypes.Length; ++i)
  88. if (parameterTypes [i] == null)
  89. throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
  90. }
  91. this.name = name;
  92. this.attributes = attributes | MethodAttributes.Static;
  93. this.callingConvention = callingConvention;
  94. this.returnType = returnType;
  95. this.parameters = parameterTypes;
  96. this.owner = owner;
  97. this.module = m;
  98. this.skipVisibility = skipVisibility;
  99. }
  100. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  101. private extern void create_dynamic_method (DynamicMethod m);
  102. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  103. private extern void destroy_dynamic_method (DynamicMethod m);
  104. private void CreateDynMethod () {
  105. if (mhandle.Value == IntPtr.Zero) {
  106. if (ilgen == null || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0))
  107. throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
  108. ilgen.label_fixup ();
  109. // Have to create all DynamicMethods referenced by this one
  110. try {
  111. // Used to avoid cycles
  112. creating = true;
  113. if (refs != null) {
  114. for (int i = 0; i < refs.Length; ++i) {
  115. if (refs [i] is DynamicMethod) {
  116. DynamicMethod m = (DynamicMethod)refs [i];
  117. if (!m.creating)
  118. m.CreateDynMethod ();
  119. }
  120. }
  121. }
  122. } finally {
  123. creating = false;
  124. }
  125. create_dynamic_method (this);
  126. }
  127. }
  128. ~DynamicMethod ()
  129. {
  130. destroy_dynamic_method (this);
  131. }
  132. [ComVisible (true)]
  133. public Delegate CreateDelegate (Type delegateType)
  134. {
  135. if (delegateType == null)
  136. throw new ArgumentNullException ("delegateType");
  137. if (deleg != null)
  138. return deleg;
  139. CreateDynMethod ();
  140. deleg = Delegate.CreateDelegate (delegateType, this);
  141. return deleg;
  142. }
  143. [ComVisible (true)]
  144. public Delegate CreateDelegate (Type delegateType, object target)
  145. {
  146. if (delegateType == null)
  147. throw new ArgumentNullException ("delegateType");
  148. CreateDynMethod ();
  149. /* Can't cache the delegate since it is different for each target */
  150. return Delegate.CreateDelegate (delegateType, target, this);
  151. }
  152. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  153. {
  154. //
  155. // Extension: Mono allows position == 0 for the return attribute
  156. //
  157. if ((position < 0) || (position > parameters.Length))
  158. throw new ArgumentOutOfRangeException ("position");
  159. RejectIfCreated ();
  160. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  161. if (pinfo == null)
  162. pinfo = new ParameterBuilder [parameters.Length + 1];
  163. pinfo [position] = pb;
  164. return pb;
  165. }
  166. public override MethodInfo GetBaseDefinition () {
  167. return this;
  168. }
  169. [MonoTODO("Not implemented")]
  170. public override object[] GetCustomAttributes (bool inherit) {
  171. throw new NotImplementedException ();
  172. }
  173. [MonoTODO("Not implemented")]
  174. public override object[] GetCustomAttributes (Type attributeType,
  175. bool inherit) {
  176. throw new NotImplementedException ();
  177. }
  178. [MonoTODO("Not implemented")]
  179. public DynamicILInfo GetDynamicILInfo () {
  180. throw new NotImplementedException ();
  181. }
  182. public ILGenerator GetILGenerator () {
  183. return GetILGenerator (64);
  184. }
  185. public ILGenerator GetILGenerator (int size) {
  186. if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
  187. MethodImplAttributes.IL) ||
  188. ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
  189. MethodImplAttributes.Managed))
  190. throw new InvalidOperationException ("Method body should not exist.");
  191. if (ilgen != null)
  192. return ilgen;
  193. ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
  194. return ilgen;
  195. }
  196. public override MethodImplAttributes GetMethodImplementationFlags () {
  197. return MethodImplAttributes.IL | MethodImplAttributes.Managed;
  198. }
  199. public override ParameterInfo[] GetParameters () {
  200. if (parameters == null)
  201. return new ParameterInfo [0];
  202. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  203. for (int i = 0; i < parameters.Length; i++) {
  204. retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
  205. }
  206. return retval;
  207. }
  208. /*
  209. public override object Invoke (object obj, object[] parameters) {
  210. CreateDynMethod ();
  211. if (method == null)
  212. method = new MonoMethod (mhandle);
  213. return method.Invoke (obj, parameters);
  214. }
  215. */
  216. public override object Invoke (object obj, BindingFlags invokeAttr,
  217. Binder binder, object[] parameters,
  218. CultureInfo culture) {
  219. CreateDynMethod ();
  220. if (method == null)
  221. method = new MonoMethod (mhandle);
  222. return method.Invoke (obj, parameters);
  223. }
  224. [MonoTODO("Not implemented")]
  225. public override bool IsDefined (Type attributeType, bool inherit) {
  226. throw new NotImplementedException ();
  227. }
  228. public override string ToString () {
  229. string parms = String.Empty;
  230. ParameterInfo[] p = GetParameters ();
  231. for (int i = 0; i < p.Length; ++i) {
  232. if (i > 0)
  233. parms = parms + ", ";
  234. parms = parms + p [i].ParameterType.Name;
  235. }
  236. return ReturnType.Name+" "+Name+"("+parms+")";
  237. }
  238. public override MethodAttributes Attributes {
  239. get {
  240. return attributes;
  241. }
  242. }
  243. public override CallingConventions CallingConvention {
  244. get {
  245. return callingConvention;
  246. }
  247. }
  248. public override Type DeclaringType {
  249. get {
  250. return null;
  251. }
  252. }
  253. public bool InitLocals {
  254. get {
  255. return init_locals;
  256. }
  257. set {
  258. init_locals = value;
  259. }
  260. }
  261. public override RuntimeMethodHandle MethodHandle {
  262. get {
  263. return mhandle;
  264. }
  265. }
  266. public override Module Module {
  267. get {
  268. return module;
  269. }
  270. }
  271. public override string Name {
  272. get {
  273. return name;
  274. }
  275. }
  276. public override Type ReflectedType {
  277. get {
  278. return null;
  279. }
  280. }
  281. [MonoTODO("Not implemented")]
  282. public override ParameterInfo ReturnParameter {
  283. get {
  284. throw new NotImplementedException ();
  285. }
  286. }
  287. public override Type ReturnType {
  288. get {
  289. return returnType;
  290. }
  291. }
  292. [MonoTODO("Not implemented")]
  293. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  294. get {
  295. throw new NotImplementedException ();
  296. }
  297. }
  298. /*
  299. public override int MetadataToken {
  300. get {
  301. return 0;
  302. }
  303. }
  304. */
  305. private void RejectIfCreated () {
  306. if (mhandle.Value != IntPtr.Zero)
  307. throw new InvalidOperationException ("Type definition of the method is complete.");
  308. }
  309. private Exception NotSupported () {
  310. return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
  311. }
  312. internal int AddRef (object reference) {
  313. if (refs == null)
  314. refs = new object [4];
  315. if (nrefs >= refs.Length - 1) {
  316. object [] new_refs = new object [refs.Length * 2];
  317. System.Array.Copy (refs, new_refs, refs.Length);
  318. refs = new_refs;
  319. }
  320. refs [nrefs] = reference;
  321. /* Reserved by the runtime */
  322. refs [nrefs + 1] = null;
  323. nrefs += 2;
  324. return nrefs - 1;
  325. }
  326. }
  327. internal class DynamicMethodTokenGenerator : TokenGenerator {
  328. private DynamicMethod m;
  329. public DynamicMethodTokenGenerator (DynamicMethod m) {
  330. this.m = m;
  331. }
  332. public int GetToken (string str) {
  333. return m.AddRef (str);
  334. }
  335. public int GetToken (MethodInfo method, Type[] opt_param_types) {
  336. throw new InvalidOperationException ();
  337. }
  338. public int GetToken (MemberInfo member) {
  339. return m.AddRef (member);
  340. }
  341. public int GetToken (SignatureHelper helper) {
  342. return m.AddRef (helper);
  343. }
  344. }
  345. }
  346. #endif