DynamicMethod.cs 8.3 KB

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