DynamicMethod.cs 10 KB

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