DynamicMethod.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. [MonoTODO]
  125. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  126. {
  127. //
  128. // Extension: Mono allows position == 0 for the return attribute
  129. //
  130. if ((position < 0) || (position > parameters.Length))
  131. throw new ArgumentOutOfRangeException ("position");
  132. RejectIfCreated ();
  133. throw new NotImplementedException ();
  134. }
  135. public override MethodInfo GetBaseDefinition () {
  136. return this;
  137. }
  138. [MonoTODO]
  139. public override object[] GetCustomAttributes (bool inherit) {
  140. throw new NotImplementedException ();
  141. }
  142. [MonoTODO]
  143. public override object[] GetCustomAttributes (Type attributeType,
  144. bool inherit) {
  145. throw new NotImplementedException ();
  146. }
  147. public ILGenerator GetILGenerator () {
  148. return GetILGenerator (64);
  149. }
  150. public ILGenerator GetILGenerator (int size) {
  151. if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) !=
  152. MethodImplAttributes.IL) ||
  153. ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) !=
  154. MethodImplAttributes.Managed))
  155. throw new InvalidOperationException ("Method body should not exist.");
  156. if (ilgen != null)
  157. return ilgen;
  158. ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
  159. return ilgen;
  160. }
  161. public override MethodImplAttributes GetMethodImplementationFlags () {
  162. return MethodImplAttributes.IL | MethodImplAttributes.Managed;
  163. }
  164. public override ParameterInfo[] GetParameters () {
  165. if (parameters == null)
  166. return new ParameterInfo [0];
  167. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  168. for (int i = 0; i < parameters.Length; i++) {
  169. retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i + 1], parameters [i], this, i + 1);
  170. }
  171. return retval;
  172. }
  173. public override object Invoke (object obj, object[] parameters) {
  174. CreateDynMethod ();
  175. if (method == null)
  176. method = new MonoMethod (mhandle);
  177. return method.Invoke (obj, parameters);
  178. }
  179. public override object Invoke (object obj, BindingFlags invokeAttr,
  180. Binder binder, object[] parameters,
  181. CultureInfo culture) {
  182. CreateDynMethod ();
  183. if (method == null)
  184. method = new MonoMethod (mhandle);
  185. return method.Invoke (obj, parameters);
  186. }
  187. [MonoTODO]
  188. public override bool IsDefined (Type attributeType, bool inherit) {
  189. throw new NotImplementedException ();
  190. }
  191. public override string ToString () {
  192. string parms = "";
  193. ParameterInfo[] p = GetParameters ();
  194. for (int i = 0; i < p.Length; ++i) {
  195. if (i > 0)
  196. parms = parms + ", ";
  197. parms = parms + p [i].ParameterType.Name;
  198. }
  199. return ReturnType.Name+" "+Name+"("+parms+")";
  200. }
  201. public override MethodAttributes Attributes {
  202. get {
  203. return attributes;
  204. }
  205. }
  206. public override CallingConventions CallingConvention {
  207. get {
  208. return callingConvention;
  209. }
  210. }
  211. public override Type DeclaringType {
  212. get {
  213. return null;
  214. }
  215. }
  216. public bool InitLocals {
  217. get {
  218. return init_locals;
  219. }
  220. set {
  221. init_locals = value;
  222. }
  223. }
  224. public override RuntimeMethodHandle MethodHandle {
  225. get {
  226. return mhandle;
  227. }
  228. }
  229. public override Module Module {
  230. get {
  231. return module;
  232. }
  233. }
  234. public override string Name {
  235. get {
  236. return name;
  237. }
  238. }
  239. public override Type ReflectedType {
  240. get {
  241. return null;
  242. }
  243. }
  244. [MonoTODO]
  245. public ParameterInfo ReturnParameter {
  246. get {
  247. throw new NotImplementedException ();
  248. }
  249. }
  250. public override Type ReturnType {
  251. get {
  252. return returnType;
  253. }
  254. }
  255. [MonoTODO]
  256. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  257. get {
  258. throw new NotImplementedException ();
  259. }
  260. }
  261. public override int MetadataToken {
  262. get {
  263. return 0;
  264. }
  265. }
  266. private void RejectIfCreated () {
  267. if (mhandle.Value != IntPtr.Zero)
  268. throw new InvalidOperationException ("Type definition of the method is complete.");
  269. }
  270. private Exception NotSupported () {
  271. return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
  272. }
  273. internal int AddRef (object reference) {
  274. if (refs == null)
  275. refs = new object [4];
  276. if (nrefs >= refs.Length - 1) {
  277. object [] new_refs = new object [refs.Length * 2];
  278. System.Array.Copy (refs, new_refs, refs.Length);
  279. refs = new_refs;
  280. }
  281. refs [nrefs] = reference;
  282. /* Reserved by the runtime */
  283. refs [nrefs + 1] = null;
  284. nrefs += 2;
  285. return nrefs - 1;
  286. }
  287. }
  288. internal class DynamicMethodTokenGenerator : TokenGenerator {
  289. private DynamicMethod m;
  290. public DynamicMethodTokenGenerator (DynamicMethod m) {
  291. this.m = m;
  292. }
  293. public int GetToken (string str) {
  294. return m.AddRef (str);
  295. }
  296. public int GetToken (MethodInfo method, Type[] opt_param_types) {
  297. throw new InvalidOperationException ();
  298. }
  299. public int GetToken (MemberInfo member) {
  300. return m.AddRef (member);
  301. }
  302. public int GetToken (SignatureHelper helper) {
  303. return m.AddRef (helper);
  304. }
  305. }
  306. }
  307. #endif