MethodBuilder.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. //
  2. // System.Reflection.Emit/MethodBuilder.cs
  3. //
  4. // Author:
  5. // Paolo Molaro ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if MONO_FEATURE_SRE
  32. using System;
  33. using System.Reflection;
  34. using System.Reflection.Emit;
  35. using System.Globalization;
  36. using System.Security;
  37. using System.Security.Permissions;
  38. using System.Runtime.CompilerServices;
  39. using System.Runtime.InteropServices;
  40. using System.Diagnostics.SymbolStore;
  41. using System.Collections.Generic;
  42. namespace System.Reflection.Emit
  43. {
  44. #if !MOBILE
  45. [ComVisible (true)]
  46. [ComDefaultInterface (typeof (_MethodBuilder))]
  47. [ClassInterface (ClassInterfaceType.None)]
  48. partial class MethodBuilder : _MethodBuilder
  49. {
  50. void _MethodBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  51. {
  52. throw new NotImplementedException ();
  53. }
  54. void _MethodBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  55. {
  56. throw new NotImplementedException ();
  57. }
  58. void _MethodBuilder.GetTypeInfoCount (out uint pcTInfo)
  59. {
  60. throw new NotImplementedException ();
  61. }
  62. void _MethodBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  63. {
  64. throw new NotImplementedException ();
  65. }
  66. }
  67. #endif
  68. [StructLayout (LayoutKind.Sequential)]
  69. public sealed partial class MethodBuilder : MethodInfo
  70. {
  71. #pragma warning disable 169, 414
  72. private RuntimeMethodHandle mhandle;
  73. private Type rtype;
  74. internal Type[] parameters;
  75. private MethodAttributes attrs; /* It's used directly by MCS */
  76. private MethodImplAttributes iattrs;
  77. private string name;
  78. private int table_idx;
  79. private byte[] code;
  80. private ILGenerator ilgen;
  81. private TypeBuilder type;
  82. internal ParameterBuilder[] pinfo;
  83. private CustomAttributeBuilder[] cattrs;
  84. private MethodInfo[] override_methods;
  85. private string pi_dll;
  86. private string pi_entry;
  87. private CharSet charset;
  88. private uint extra_flags; /* this encodes set_last_error etc */
  89. private CallingConvention native_cc;
  90. private CallingConventions call_conv;
  91. private bool init_locals = true;
  92. private IntPtr generic_container;
  93. internal GenericTypeParameterBuilder[] generic_params;
  94. private Type[] returnModReq;
  95. private Type[] returnModOpt;
  96. private Type[][] paramModReq;
  97. private Type[][] paramModOpt;
  98. private RefEmitPermissionSet[] permissions;
  99. #pragma warning restore 169, 414
  100. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
  101. {
  102. this.name = name;
  103. this.attrs = attributes;
  104. this.call_conv = callingConvention;
  105. this.rtype = returnType;
  106. this.returnModReq = returnModReq;
  107. this.returnModOpt = returnModOpt;
  108. this.paramModReq = paramModReq;
  109. this.paramModOpt = paramModOpt;
  110. // The MSDN docs does not specify this, but the MS MethodBuilder
  111. // appends a HasThis flag if the method is not static
  112. if ((attributes & MethodAttributes.Static) == 0)
  113. this.call_conv |= CallingConventions.HasThis;
  114. if (parameterTypes != null) {
  115. for (int i = 0; i < parameterTypes.Length; ++i)
  116. if (parameterTypes [i] == null)
  117. throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
  118. this.parameters = new Type [parameterTypes.Length];
  119. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  120. }
  121. type = tb;
  122. table_idx = get_next_table_index (this, 0x06, 1);
  123. ((ModuleBuilder)tb.Module).RegisterToken (this, GetToken ().Token);
  124. }
  125. internal MethodBuilder (TypeBuilder tb, string name, MethodAttributes attributes,
  126. CallingConventions callingConvention, Type returnType, Type[] returnModReq, Type[] returnModOpt, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt,
  127. String dllName, String entryName, CallingConvention nativeCConv, CharSet nativeCharset)
  128. : this (tb, name, attributes, callingConvention, returnType, returnModReq, returnModOpt, parameterTypes, paramModReq, paramModOpt)
  129. {
  130. pi_dll = dllName;
  131. pi_entry = entryName;
  132. native_cc = nativeCConv;
  133. charset = nativeCharset;
  134. }
  135. public override bool ContainsGenericParameters {
  136. get { throw new NotSupportedException (); }
  137. }
  138. public bool InitLocals {
  139. get {return init_locals;}
  140. set {init_locals = value;}
  141. }
  142. internal TypeBuilder TypeBuilder {
  143. get {return type;}
  144. }
  145. public override RuntimeMethodHandle MethodHandle {
  146. get {
  147. throw NotSupported ();
  148. }
  149. }
  150. internal RuntimeMethodHandle MethodHandleInternal {
  151. get {
  152. return mhandle;
  153. }
  154. }
  155. public override Type ReturnType {
  156. get { return rtype; }
  157. }
  158. public override Type ReflectedType {
  159. get { return type; }
  160. }
  161. public override Type DeclaringType {
  162. get { return type; }
  163. }
  164. public override string Name {
  165. get { return name; }
  166. }
  167. public override MethodAttributes Attributes {
  168. get { return attrs; }
  169. }
  170. public override ICustomAttributeProvider ReturnTypeCustomAttributes {
  171. get { return null; }
  172. }
  173. public override CallingConventions CallingConvention {
  174. get { return call_conv; }
  175. }
  176. [MonoTODO("Not implemented")]
  177. public string Signature {
  178. get {
  179. throw new NotImplementedException ();
  180. }
  181. }
  182. /* Used by mcs */
  183. internal bool BestFitMapping {
  184. set {
  185. extra_flags = (uint) ((extra_flags & ~0x30) | (uint)(value ? 0x10 : 0x20));
  186. }
  187. }
  188. /* Used by mcs */
  189. internal bool ThrowOnUnmappableChar {
  190. set {
  191. extra_flags = (uint) ((extra_flags & ~0x3000) | (uint)(value ? 0x1000 : 0x2000));
  192. }
  193. }
  194. /* Used by mcs */
  195. internal bool ExactSpelling {
  196. set {
  197. extra_flags = (uint) ((extra_flags & ~0x01) | (uint)(value ? 0x01 : 0x00));
  198. }
  199. }
  200. /* Used by mcs */
  201. internal bool SetLastError {
  202. set {
  203. extra_flags = (uint) ((extra_flags & ~0x40) | (uint)(value ? 0x40 : 0x00));
  204. }
  205. }
  206. public MethodToken GetToken()
  207. {
  208. return new MethodToken(0x06000000 | table_idx);
  209. }
  210. public override MethodInfo GetBaseDefinition()
  211. {
  212. return this;
  213. }
  214. public override MethodImplAttributes GetMethodImplementationFlags()
  215. {
  216. return iattrs;
  217. }
  218. public override ParameterInfo[] GetParameters()
  219. {
  220. if (!type.is_created)
  221. throw NotSupported ();
  222. return GetParametersInternal ();
  223. }
  224. internal override ParameterInfo[] GetParametersInternal ()
  225. {
  226. if (parameters == null)
  227. return null;
  228. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  229. for (int i = 0; i < parameters.Length; i++) {
  230. retval [i] = MonoParameterInfo.New (pinfo?[i + 1], parameters [i], this, i + 1);
  231. }
  232. return retval;
  233. }
  234. internal override int GetParametersCount ()
  235. {
  236. if (parameters == null)
  237. return 0;
  238. return parameters.Length;
  239. }
  240. internal override Type GetParameterType (int pos) {
  241. return parameters [pos];
  242. }
  243. internal MethodBase RuntimeResolve () {
  244. return type.RuntimeResolve ().GetMethod (this);
  245. }
  246. public Module GetModule ()
  247. {
  248. return type.Module;
  249. }
  250. public void CreateMethodBody (byte[] il, int count)
  251. {
  252. if ((il != null) && ((count < 0) || (count > il.Length)))
  253. throw new ArgumentOutOfRangeException ("Index was out of range. Must be non-negative and less than the size of the collection.");
  254. if ((code != null) || type.is_created)
  255. throw new InvalidOperationException ("Type definition of the method is complete.");
  256. if (il == null)
  257. code = null;
  258. else {
  259. code = new byte [count];
  260. System.Array.Copy(il, code, count);
  261. }
  262. }
  263. public void SetMethodBody (byte[] il, int maxStack, byte[] localSignature,
  264. IEnumerable<ExceptionHandler> exceptionHandlers, IEnumerable<int> tokenFixups)
  265. {
  266. var ilgen = GetILGenerator ();
  267. ilgen.Init (il, maxStack, localSignature, exceptionHandlers, tokenFixups);
  268. }
  269. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  270. {
  271. throw NotSupported ();
  272. }
  273. public override bool IsDefined (Type attributeType, bool inherit)
  274. {
  275. throw NotSupported ();
  276. }
  277. public override object[] GetCustomAttributes (bool inherit)
  278. {
  279. /*
  280. * On MS.NET, this always returns not_supported, but we can't do this
  281. * since there would be no way to obtain custom attributes of
  282. * dynamically created ctors.
  283. */
  284. if (type.is_created)
  285. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  286. else
  287. throw NotSupported ();
  288. }
  289. public override object[] GetCustomAttributes (Type attributeType, bool inherit)
  290. {
  291. if (type.is_created)
  292. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  293. else
  294. throw NotSupported ();
  295. }
  296. public ILGenerator GetILGenerator ()
  297. {
  298. return GetILGenerator (64);
  299. }
  300. public ILGenerator GetILGenerator (int size)
  301. {
  302. if (((iattrs & MethodImplAttributes.CodeTypeMask) !=
  303. MethodImplAttributes.IL) ||
  304. ((iattrs & MethodImplAttributes.ManagedMask) !=
  305. MethodImplAttributes.Managed))
  306. throw new InvalidOperationException ("Method body should not exist.");
  307. if (ilgen != null)
  308. return ilgen;
  309. ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), size);
  310. return ilgen;
  311. }
  312. public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
  313. {
  314. RejectIfCreated ();
  315. //
  316. // Extension: Mono allows position == 0 for the return attribute
  317. //
  318. if ((position < 0) || parameters == null || (position > parameters.Length))
  319. throw new ArgumentOutOfRangeException ("position");
  320. ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
  321. if (pinfo == null)
  322. pinfo = new ParameterBuilder [parameters.Length + 1];
  323. pinfo [position] = pb;
  324. return pb;
  325. }
  326. internal void check_override ()
  327. {
  328. if (override_methods != null) {
  329. foreach (var m in override_methods) {
  330. if (m.IsVirtual && !IsVirtual)
  331. throw new TypeLoadException (String.Format("Method '{0}' override '{1}' but it is not virtual", name, m));
  332. }
  333. }
  334. }
  335. internal void fixup ()
  336. {
  337. if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
  338. // do not allow zero length method body on MS.NET 2.0 (and higher)
  339. if (((ilgen == null) || (ilgen.ILOffset == 0)) && (code == null || code.Length == 0))
  340. throw new InvalidOperationException (
  341. String.Format ("Method '{0}.{1}' does not have a method body.",
  342. DeclaringType.FullName, Name));
  343. }
  344. if (ilgen != null)
  345. ilgen.label_fixup (this);
  346. }
  347. internal void ResolveUserTypes () {
  348. rtype = TypeBuilder.ResolveUserType (rtype);
  349. TypeBuilder.ResolveUserTypes (parameters);
  350. TypeBuilder.ResolveUserTypes (returnModReq);
  351. TypeBuilder.ResolveUserTypes (returnModOpt);
  352. if (paramModReq != null) {
  353. foreach (var types in paramModReq)
  354. TypeBuilder.ResolveUserTypes (types);
  355. }
  356. if (paramModOpt != null) {
  357. foreach (var types in paramModOpt)
  358. TypeBuilder.ResolveUserTypes (types);
  359. }
  360. }
  361. internal void FixupTokens (Dictionary<int, int> token_map, Dictionary<int, MemberInfo> member_map) {
  362. if (ilgen != null)
  363. ilgen.FixupTokens (token_map, member_map);
  364. }
  365. internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
  366. {
  367. if (ilgen != null && ilgen.HasDebugInfo) {
  368. SymbolToken token = new SymbolToken (GetToken().Token);
  369. symbolWriter.OpenMethod (token);
  370. symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
  371. ilgen.GenerateDebugInfo (symbolWriter);
  372. symbolWriter.CloseMethod ();
  373. }
  374. }
  375. public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
  376. {
  377. if (customBuilder == null)
  378. throw new ArgumentNullException ("customBuilder");
  379. switch (customBuilder.Ctor.ReflectedType.FullName) {
  380. case "System.Runtime.CompilerServices.MethodImplAttribute":
  381. byte[] data = customBuilder.Data;
  382. int impla; // the (stupid) ctor takes a short or an int ...
  383. impla = (int)data [2];
  384. impla |= ((int)data [3]) << 8;
  385. iattrs |= (MethodImplAttributes)impla;
  386. return;
  387. case "System.Runtime.InteropServices.DllImportAttribute":
  388. CustomAttributeBuilder.CustomAttributeInfo attr = CustomAttributeBuilder.decode_cattr (customBuilder);
  389. bool preserveSig = true;
  390. /*
  391. * It would be easier to construct a DllImportAttribute from
  392. * the custom attribute builder, but the DllImportAttribute
  393. * does not contain all the information required here, ie.
  394. * - some parameters, like BestFitMapping has three values
  395. * ("on", "off", "missing"), but DllImportAttribute only
  396. * contains two (on/off).
  397. * - PreserveSig is true by default, while it is false by
  398. * default in DllImportAttribute.
  399. */
  400. pi_dll = (string)attr.ctorArgs[0];
  401. if (pi_dll == null || pi_dll.Length == 0)
  402. throw new ArgumentException ("DllName cannot be empty");
  403. native_cc = System.Runtime.InteropServices.CallingConvention.Winapi;
  404. for (int i = 0; i < attr.namedParamNames.Length; ++i) {
  405. string name = attr.namedParamNames [i];
  406. object value = attr.namedParamValues [i];
  407. if (name == "CallingConvention")
  408. native_cc = (CallingConvention)value;
  409. else if (name == "CharSet")
  410. charset = (CharSet)value;
  411. else if (name == "EntryPoint")
  412. pi_entry = (string)value;
  413. else if (name == "ExactSpelling")
  414. ExactSpelling = (bool)value;
  415. else if (name == "SetLastError")
  416. SetLastError = (bool)value;
  417. else if (name == "PreserveSig")
  418. preserveSig = (bool)value;
  419. else if (name == "BestFitMapping")
  420. BestFitMapping = (bool)value;
  421. else if (name == "ThrowOnUnmappableChar")
  422. ThrowOnUnmappableChar = (bool)value;
  423. }
  424. attrs |= MethodAttributes.PinvokeImpl;
  425. if (preserveSig)
  426. iattrs |= MethodImplAttributes.PreserveSig;
  427. return;
  428. case "System.Runtime.InteropServices.PreserveSigAttribute":
  429. iattrs |= MethodImplAttributes.PreserveSig;
  430. return;
  431. case "System.Runtime.CompilerServices.SpecialNameAttribute":
  432. attrs |= MethodAttributes.SpecialName;
  433. return;
  434. case "System.Security.SuppressUnmanagedCodeSecurityAttribute":
  435. attrs |= MethodAttributes.HasSecurity;
  436. break;
  437. }
  438. if (cattrs != null) {
  439. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  440. cattrs.CopyTo (new_array, 0);
  441. new_array [cattrs.Length] = customBuilder;
  442. cattrs = new_array;
  443. } else {
  444. cattrs = new CustomAttributeBuilder [1];
  445. cattrs [0] = customBuilder;
  446. }
  447. }
  448. [ComVisible (true)]
  449. public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
  450. {
  451. if (con == null)
  452. throw new ArgumentNullException ("con");
  453. if (binaryAttribute == null)
  454. throw new ArgumentNullException ("binaryAttribute");
  455. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  456. }
  457. public void SetImplementationFlags (MethodImplAttributes attributes)
  458. {
  459. RejectIfCreated ();
  460. iattrs = attributes;
  461. }
  462. public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
  463. {
  464. #if !MOBILE
  465. if (pset == null)
  466. throw new ArgumentNullException ("pset");
  467. if ((action == SecurityAction.RequestMinimum) ||
  468. (action == SecurityAction.RequestOptional) ||
  469. (action == SecurityAction.RequestRefuse))
  470. throw new ArgumentOutOfRangeException ("Request* values are not permitted", "action");
  471. RejectIfCreated ();
  472. if (permissions != null) {
  473. /* Check duplicate actions */
  474. foreach (RefEmitPermissionSet set in permissions)
  475. if (set.action == action)
  476. throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
  477. RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
  478. permissions.CopyTo (new_array, 0);
  479. permissions = new_array;
  480. }
  481. else
  482. permissions = new RefEmitPermissionSet [1];
  483. permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
  484. attrs |= MethodAttributes.HasSecurity;
  485. #endif
  486. }
  487. [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
  488. public void SetMarshal (UnmanagedMarshal unmanagedMarshal)
  489. {
  490. RejectIfCreated ();
  491. throw new NotImplementedException ();
  492. }
  493. [MonoTODO]
  494. public void SetSymCustomAttribute (string name, byte[] data)
  495. {
  496. RejectIfCreated ();
  497. throw new NotImplementedException ();
  498. }
  499. public override string ToString()
  500. {
  501. return "MethodBuilder [" + type.Name + "::" + name + "]";
  502. }
  503. [MonoTODO]
  504. public override bool Equals (object obj)
  505. {
  506. return base.Equals (obj);
  507. }
  508. public override int GetHashCode ()
  509. {
  510. return name.GetHashCode ();
  511. }
  512. internal override int get_next_table_index (object obj, int table, int count)
  513. {
  514. return type.get_next_table_index (obj, table, count);
  515. }
  516. void ExtendArray<T> (ref T[] array, T elem) {
  517. if (array == null) {
  518. array = new T [1];
  519. } else {
  520. var newa = new T [array.Length + 1];
  521. Array.Copy (array, newa, array.Length);
  522. array = newa;
  523. }
  524. array [array.Length - 1] = elem;
  525. }
  526. internal void set_override (MethodInfo mdecl)
  527. {
  528. ExtendArray<MethodInfo> (ref override_methods, mdecl);
  529. }
  530. private void RejectIfCreated ()
  531. {
  532. if (type.is_created)
  533. throw new InvalidOperationException ("Type definition of the method is complete.");
  534. }
  535. private Exception NotSupported ()
  536. {
  537. return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
  538. }
  539. public override MethodInfo MakeGenericMethod (params Type [] typeArguments)
  540. {
  541. if (!IsGenericMethodDefinition)
  542. throw new InvalidOperationException ("Method is not a generic method definition");
  543. if (typeArguments == null)
  544. throw new ArgumentNullException ("typeArguments");
  545. if (generic_params.Length != typeArguments.Length)
  546. throw new ArgumentException ("Incorrect length", "typeArguments");
  547. foreach (Type type in typeArguments) {
  548. if (type == null)
  549. throw new ArgumentNullException ("typeArguments");
  550. }
  551. return new MethodOnTypeBuilderInst (this, typeArguments);
  552. }
  553. public override bool IsGenericMethodDefinition {
  554. get {
  555. return generic_params != null;
  556. }
  557. }
  558. public override bool IsGenericMethod {
  559. get {
  560. return generic_params != null;
  561. }
  562. }
  563. public override MethodInfo GetGenericMethodDefinition ()
  564. {
  565. if (!IsGenericMethodDefinition)
  566. throw new InvalidOperationException ();
  567. return this;
  568. }
  569. public override Type[] GetGenericArguments ()
  570. {
  571. if (generic_params == null)
  572. return null;
  573. Type[] result = new Type [generic_params.Length];
  574. for (int i = 0; i < generic_params.Length; i++)
  575. result [i] = generic_params [i];
  576. return result;
  577. }
  578. public GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names)
  579. {
  580. if (names == null)
  581. throw new ArgumentNullException ("names");
  582. if (names.Length == 0)
  583. throw new ArgumentException ("names");
  584. generic_params = new GenericTypeParameterBuilder [names.Length];
  585. for (int i = 0; i < names.Length; i++) {
  586. string item = names [i];
  587. if (item == null)
  588. throw new ArgumentNullException ("names");
  589. generic_params [i] = new GenericTypeParameterBuilder (type, this, item, i);
  590. }
  591. return generic_params;
  592. }
  593. public void SetReturnType (Type returnType)
  594. {
  595. rtype = returnType;
  596. }
  597. public void SetParameters (params Type[] parameterTypes)
  598. {
  599. if (parameterTypes != null) {
  600. for (int i = 0; i < parameterTypes.Length; ++i)
  601. if (parameterTypes [i] == null)
  602. throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
  603. this.parameters = new Type [parameterTypes.Length];
  604. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  605. }
  606. }
  607. public void SetSignature (Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers)
  608. {
  609. SetReturnType (returnType);
  610. SetParameters (parameterTypes);
  611. this.returnModReq = returnTypeRequiredCustomModifiers;
  612. this.returnModOpt = returnTypeOptionalCustomModifiers;
  613. this.paramModReq = parameterTypeRequiredCustomModifiers;
  614. this.paramModOpt = parameterTypeOptionalCustomModifiers;
  615. }
  616. public override Module Module {
  617. get {
  618. return GetModule ();
  619. }
  620. }
  621. public override ParameterInfo ReturnParameter {
  622. get { return base.ReturnParameter; }
  623. }
  624. }
  625. }
  626. #endif