DynamicMethod.cs 9.9 KB

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