DynamicMethod.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #endregion
  58. private Delegate deleg;
  59. private MonoMethod method;
  60. private ParameterBuilder[] pinfo;
  61. internal bool creating;
  62. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
  63. }
  64. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner) : this (name, returnType, parameterTypes, owner, false) {
  65. }
  66. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, m, skipVisibility) {
  67. }
  68. public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Type owner, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, owner, skipVisibility) {
  69. }
  70. 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) {
  71. }
  72. public DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] parameterTypes, Module m, bool skipVisibility) {
  73. if (name == null)
  74. throw new ArgumentNullException ("name");
  75. if (name == String.Empty)
  76. throw new ArgumentException ("Name can't be empty", "name");
  77. if (returnType == null)
  78. throw new ArgumentNullException ("returnType");
  79. if (m == null)
  80. throw new ArgumentNullException ("m");
  81. if (returnType.IsByRef)
  82. throw new ArgumentException ("Return type can't be a byref type", "returnType");
  83. if (parameterTypes != null) {
  84. for (int i = 0; i < parameterTypes.Length; ++i)
  85. if (parameterTypes [i] == null)
  86. throw new ArgumentException ("Parameter " + i + " is null", "parameterTypes");
  87. }
  88. this.name = name;
  89. this.attributes = attributes | MethodAttributes.Static;
  90. this.callingConvention = callingConvention;
  91. this.returnType = returnType;
  92. this.parameters = parameterTypes;
  93. this.module = m;
  94. this.skipVisibility = skipVisibility;
  95. }
  96. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  97. private extern void create_dynamic_method (DynamicMethod m);
  98. private void CreateDynMethod () {
  99. if (mhandle.Value == IntPtr.Zero) {
  100. if (ilgen == null)
  101. throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
  102. ilgen.label_fixup ();
  103. // Have to create all DynamicMethods referenced by this one
  104. try {
  105. // Used to avoid cycles
  106. creating = true;
  107. if (refs != null) {
  108. for (int i = 0; i < refs.Length; ++i) {
  109. if (refs [i] is DynamicMethod) {
  110. DynamicMethod m = (DynamicMethod)refs [i];
  111. if (!m.creating)
  112. m.CreateDynMethod ();
  113. }
  114. }
  115. }
  116. } finally {
  117. creating = false;
  118. }
  119. create_dynamic_method (this);
  120. }
  121. }
  122. [ComVisible (true)]
  123. public Delegate CreateDelegate (Type delegateType)
  124. {
  125. if (delegateType == null)
  126. throw new ArgumentNullException ("delegateType");
  127. if (deleg != null)
  128. return deleg;
  129. CreateDynMethod ();
  130. deleg = Delegate.CreateDelegate (delegateType, this);
  131. return deleg;
  132. }
  133. [ComVisible (true)]
  134. public Delegate CreateDelegate (Type delegateType, object target)
  135. {
  136. if (delegateType == null)
  137. throw new ArgumentNullException ("delegateType");
  138. CreateDynMethod ();
  139. /* Can't cache the delegate since it is different for each target */
  140. return Delegate.CreateDelegate (delegateType, target, this);
  141. }
  142. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  143. {
  144. //
  145. // Extension: Mono allows position == 0 for the return attribute
  146. //
  147. if ((position < 0) || (position > parameters.Length))
  148. throw new ArgumentOutOfRangeException ("position");
  149. RejectIfCreated ();
  150. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  151. if (pinfo == null)
  152. pinfo = new ParameterBuilder [parameters.Length + 1];
  153. pinfo [position] = pb;
  154. return pb;
  155. }
  156. public override MethodInfo GetBaseDefinition () {
  157. return this;
  158. }
  159. [MonoTODO]
  160. public override object[] GetCustomAttributes (bool inherit) {
  161. throw new NotImplementedException ();
  162. }
  163. [MonoTODO]
  164. public override object[] GetCustomAttributes (Type attributeType,
  165. bool inherit) {
  166. throw new NotImplementedException ();
  167. }
  168. public ILGenerator GetILGenerator () {
  169. return GetILGenerator (64);
  170. }
  171. public ILGenerator GetILGenerator (int size) {
  172. if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
  173. MethodImplAttributes.IL) ||
  174. ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
  175. MethodImplAttributes.Managed))
  176. throw new InvalidOperationException ("Method body should not exist.");
  177. if (ilgen != null)
  178. return ilgen;
  179. ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
  180. return ilgen;
  181. }
  182. public override MethodImplAttributes GetMethodImplementationFlags () {
  183. return MethodImplAttributes.IL | MethodImplAttributes.Managed;
  184. }
  185. public override ParameterInfo[] GetParameters () {
  186. if (parameters == null)
  187. return new ParameterInfo [0];
  188. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  189. for (int i = 0; i < parameters.Length; i++) {
  190. retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
  191. }
  192. return retval;
  193. }
  194. public override object Invoke (object obj, object[] parameters) {
  195. CreateDynMethod ();
  196. if (method == null)
  197. method = new MonoMethod (mhandle);
  198. return method.Invoke (obj, parameters);
  199. }
  200. public override object Invoke (object obj, BindingFlags invokeAttr,
  201. Binder binder, object[] parameters,
  202. CultureInfo culture) {
  203. CreateDynMethod ();
  204. if (method == null)
  205. method = new MonoMethod (mhandle);
  206. return method.Invoke (obj, parameters);
  207. }
  208. [MonoTODO]
  209. public override bool IsDefined (Type attributeType, bool inherit) {
  210. throw new NotImplementedException ();
  211. }
  212. public override string ToString () {
  213. string parms = "";
  214. ParameterInfo[] p = GetParameters ();
  215. for (int i = 0; i < p.Length; ++i) {
  216. if (i > 0)
  217. parms = parms + ", ";
  218. parms = parms + p [i].ParameterType.Name;
  219. }
  220. return ReturnType.Name+" "+Name+"("+parms+")";
  221. }
  222. public override MethodAttributes Attributes {
  223. get {
  224. return attributes;
  225. }
  226. }
  227. public override CallingConventions CallingConvention {
  228. get {
  229. return callingConvention;
  230. }
  231. }
  232. public override Type DeclaringType {
  233. get {
  234. return null;
  235. }
  236. }
  237. public bool InitLocals {
  238. get {
  239. return init_locals;
  240. }
  241. set {
  242. init_locals = value;
  243. }
  244. }
  245. public override RuntimeMethodHandle MethodHandle {
  246. get {
  247. return mhandle;
  248. }
  249. }
  250. public override Module Module {
  251. get {
  252. return module;
  253. }
  254. }
  255. public override string Name {
  256. get {
  257. return name;
  258. }
  259. }
  260. public override Type ReflectedType {
  261. get {
  262. return null;
  263. }
  264. }
  265. [MonoTODO]
  266. public ParameterInfo ReturnParameter {
  267. get {
  268. throw new NotImplementedException ();
  269. }
  270. }
  271. public override Type ReturnType {
  272. get {
  273. return returnType;
  274. }
  275. }
  276. [MonoTODO]
  277. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  278. get {
  279. throw new NotImplementedException ();
  280. }
  281. }
  282. public override int MetadataToken {
  283. get {
  284. return 0;
  285. }
  286. }
  287. private void RejectIfCreated () {
  288. if (mhandle.Value != IntPtr.Zero)
  289. throw new InvalidOperationException ("Type definition of the method is complete.");
  290. }
  291. private Exception NotSupported () {
  292. return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
  293. }
  294. internal int AddRef (object reference) {
  295. if (refs == null)
  296. refs = new object [4];
  297. if (nrefs >= refs.Length - 1) {
  298. object [] new_refs = new object [refs.Length * 2];
  299. System.Array.Copy (refs, new_refs, refs.Length);
  300. refs = new_refs;
  301. }
  302. refs [nrefs] = reference;
  303. /* Reserved by the runtime */
  304. refs [nrefs + 1] = null;
  305. nrefs += 2;
  306. return nrefs - 1;
  307. }
  308. }
  309. internal class DynamicMethodTokenGenerator : TokenGenerator {
  310. private DynamicMethod m;
  311. public DynamicMethodTokenGenerator (DynamicMethod m) {
  312. this.m = m;
  313. }
  314. public int GetToken (string str) {
  315. return m.AddRef (str);
  316. }
  317. public int GetToken (MethodInfo method, Type[] opt_param_types) {
  318. throw new InvalidOperationException ();
  319. }
  320. public int GetToken (MemberInfo member) {
  321. return m.AddRef (member);
  322. }
  323. public int GetToken (SignatureHelper helper) {
  324. return m.AddRef (helper);
  325. }
  326. }
  327. }
  328. #endif