DynamicMethod.cs 9.5 KB

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