ConstructorBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // System.Reflection.Emit.ConstructorBuilder.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. using System;
  32. using System.Reflection;
  33. using System.Reflection.Emit;
  34. using System.Globalization;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. using System.Runtime.InteropServices;
  38. using System.Diagnostics.SymbolStore;
  39. namespace System.Reflection.Emit {
  40. [ComVisible (true)]
  41. [ComDefaultInterface (typeof (_ConstructorBuilder))]
  42. [ClassInterface (ClassInterfaceType.None)]
  43. public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder {
  44. #pragma warning disable 169, 414
  45. private RuntimeMethodHandle mhandle;
  46. private ILGenerator ilgen;
  47. internal Type[] parameters;
  48. private MethodAttributes attrs;
  49. private MethodImplAttributes iattrs;
  50. private int table_idx;
  51. private CallingConventions call_conv;
  52. private TypeBuilder type;
  53. internal ParameterBuilder[] pinfo;
  54. private CustomAttributeBuilder[] cattrs;
  55. private bool init_locals = true;
  56. private Type[][] paramModReq;
  57. private Type[][] paramModOpt;
  58. private RefEmitPermissionSet[] permissions;
  59. #pragma warning restore 169, 414
  60. internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
  61. {
  62. attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
  63. call_conv = callingConvention;
  64. if (parameterTypes != null) {
  65. for (int i = 0; i < parameterTypes.Length; ++i)
  66. if (parameterTypes [i] == null)
  67. throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
  68. this.parameters = new Type [parameterTypes.Length];
  69. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  70. }
  71. type = tb;
  72. this.paramModReq = paramModReq;
  73. this.paramModOpt = paramModOpt;
  74. table_idx = get_next_table_index (this, 0x06, true);
  75. ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
  76. }
  77. [MonoTODO]
  78. public override CallingConventions CallingConvention {
  79. get {
  80. return call_conv;
  81. }
  82. }
  83. public bool InitLocals {
  84. get {
  85. return init_locals;
  86. }
  87. set {
  88. init_locals = value;
  89. }
  90. }
  91. internal TypeBuilder TypeBuilder {
  92. get {
  93. return type;
  94. }
  95. }
  96. public override MethodImplAttributes GetMethodImplementationFlags ()
  97. {
  98. return iattrs;
  99. }
  100. public override ParameterInfo[] GetParameters ()
  101. {
  102. if (!type.is_created && !IsCompilerContext)
  103. throw not_created ();
  104. return GetParametersInternal ();
  105. }
  106. internal ParameterInfo [] GetParametersInternal ()
  107. {
  108. if (parameters == null)
  109. return new ParameterInfo [0];
  110. ParameterInfo [] retval = new ParameterInfo [parameters.Length];
  111. for (int i = 0; i < parameters.Length; i++)
  112. retval [i] = new ParameterInfo (pinfo == null ? null
  113. : pinfo [i + 1], parameters [i], this, i + 1);
  114. return retval;
  115. }
  116. internal override int GetParameterCount ()
  117. {
  118. if (parameters == null)
  119. return 0;
  120. return parameters.Length;
  121. }
  122. public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  123. {
  124. throw not_supported ();
  125. }
  126. public override object Invoke (BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
  127. {
  128. throw not_supported ();
  129. }
  130. public override RuntimeMethodHandle MethodHandle {
  131. get {
  132. throw not_supported ();
  133. }
  134. }
  135. public override MethodAttributes Attributes {
  136. get {
  137. return attrs;
  138. }
  139. }
  140. public override Type ReflectedType {
  141. get {
  142. return type;
  143. }
  144. }
  145. public override Type DeclaringType {
  146. get {
  147. return type;
  148. }
  149. }
  150. #if NET_4_0
  151. [Obsolete]
  152. #endif
  153. public Type ReturnType {
  154. get {
  155. return null;
  156. }
  157. }
  158. public override string Name {
  159. get {
  160. return (attrs & MethodAttributes.Static) != 0 ? ConstructorInfo.TypeConstructorName : ConstructorInfo.ConstructorName;
  161. }
  162. }
  163. public string Signature {
  164. get {
  165. return "constructor signature";
  166. }
  167. }
  168. public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
  169. {
  170. #if !NET_2_1
  171. if (pset == null)
  172. throw new ArgumentNullException ("pset");
  173. if ((action == SecurityAction.RequestMinimum) ||
  174. (action == SecurityAction.RequestOptional) ||
  175. (action == SecurityAction.RequestRefuse))
  176. throw new ArgumentOutOfRangeException ("action", "Request* values are not permitted");
  177. RejectIfCreated ();
  178. if (permissions != null) {
  179. /* Check duplicate actions */
  180. foreach (RefEmitPermissionSet set in permissions)
  181. if (set.action == action)
  182. throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
  183. RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
  184. permissions.CopyTo (new_array, 0);
  185. permissions = new_array;
  186. }
  187. else
  188. permissions = new RefEmitPermissionSet [1];
  189. permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
  190. attrs |= MethodAttributes.HasSecurity;
  191. #endif
  192. }
  193. public ParameterBuilder DefineParameter (int iSequence, ParameterAttributes attributes, string strParamName)
  194. {
  195. if (iSequence < 1 || iSequence > GetParameterCount ())
  196. throw new ArgumentOutOfRangeException ("iSequence");
  197. if (type.is_created)
  198. throw not_after_created ();
  199. ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
  200. if (pinfo == null)
  201. pinfo = new ParameterBuilder [parameters.Length + 1];
  202. pinfo [iSequence] = pb;
  203. return pb;
  204. }
  205. public override bool IsDefined (Type attributeType, bool inherit)
  206. {
  207. throw not_supported ();
  208. }
  209. public override object [] GetCustomAttributes (bool inherit)
  210. {
  211. /*
  212. * On MS.NET, this always returns not_supported, but we can't do this
  213. * since there would be no way to obtain custom attributes of
  214. * dynamically created ctors.
  215. */
  216. if (type.is_created && IsCompilerContext)
  217. return MonoCustomAttrs.GetCustomAttributes (this, inherit);
  218. else
  219. throw not_supported ();
  220. }
  221. public override object [] GetCustomAttributes (Type attributeType, bool inherit)
  222. {
  223. if (type.is_created && IsCompilerContext)
  224. return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
  225. else
  226. throw not_supported ();
  227. }
  228. public ILGenerator GetILGenerator ()
  229. {
  230. return GetILGenerator (64);
  231. }
  232. public ILGenerator GetILGenerator (int streamSize)
  233. {
  234. if (ilgen != null)
  235. return ilgen;
  236. ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), streamSize);
  237. return ilgen;
  238. }
  239. public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
  240. {
  241. if (customBuilder == null)
  242. throw new ArgumentNullException ("customBuilder");
  243. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  244. if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
  245. byte[] data = customBuilder.Data;
  246. int impla; // the (stupid) ctor takes a short or an int ...
  247. impla = (int)data [2];
  248. impla |= ((int)data [3]) << 8;
  249. SetImplementationFlags ((MethodImplAttributes)impla);
  250. return;
  251. }
  252. if (cattrs != null) {
  253. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  254. cattrs.CopyTo (new_array, 0);
  255. new_array [cattrs.Length] = customBuilder;
  256. cattrs = new_array;
  257. } else {
  258. cattrs = new CustomAttributeBuilder [1];
  259. cattrs [0] = customBuilder;
  260. }
  261. }
  262. [ComVisible (true)]
  263. public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
  264. {
  265. if (con == null)
  266. throw new ArgumentNullException ("con");
  267. if (binaryAttribute == null)
  268. throw new ArgumentNullException ("binaryAttribute");
  269. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  270. }
  271. public void SetImplementationFlags (MethodImplAttributes attributes)
  272. {
  273. if (type.is_created)
  274. throw not_after_created ();
  275. iattrs = attributes;
  276. }
  277. public Module GetModule ()
  278. {
  279. return type.Module;
  280. }
  281. public MethodToken GetToken ()
  282. {
  283. return new MethodToken (0x06000000 | table_idx);
  284. }
  285. [MonoTODO]
  286. public void SetSymCustomAttribute (string name, byte[] data)
  287. {
  288. if (type.is_created)
  289. throw not_after_created ();
  290. }
  291. public override Module Module {
  292. get {
  293. return base.Module;
  294. }
  295. }
  296. public override string ToString ()
  297. {
  298. return "ConstructorBuilder ['" + type.Name + "']";
  299. }
  300. internal void fixup ()
  301. {
  302. if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
  303. if ((ilgen == null) || (ilgen.ILOffset == 0))
  304. throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
  305. }
  306. if (ilgen != null)
  307. ilgen.label_fixup ();
  308. }
  309. internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
  310. {
  311. if (ilgen != null && ilgen.HasDebugInfo) {
  312. SymbolToken token = new SymbolToken (GetToken().Token);
  313. symbolWriter.OpenMethod (token);
  314. symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
  315. ilgen.GenerateDebugInfo (symbolWriter);
  316. symbolWriter.CloseMethod ();
  317. }
  318. }
  319. internal override int get_next_table_index (object obj, int table, bool inc)
  320. {
  321. return type.get_next_table_index (obj, table, inc);
  322. }
  323. private bool IsCompilerContext {
  324. get {
  325. ModuleBuilder mb = (ModuleBuilder) TypeBuilder.Module;
  326. AssemblyBuilder ab = (AssemblyBuilder) mb.Assembly;
  327. return ab.IsCompilerContext;
  328. }
  329. }
  330. private void RejectIfCreated ()
  331. {
  332. if (type.is_created)
  333. throw new InvalidOperationException ("Type definition of the method is complete.");
  334. }
  335. private Exception not_supported ()
  336. {
  337. return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
  338. }
  339. private Exception not_after_created ()
  340. {
  341. return new InvalidOperationException ("Unable to change after type has been created.");
  342. }
  343. private Exception not_created ()
  344. {
  345. return new NotSupportedException ("The type is not yet created.");
  346. }
  347. void _ConstructorBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  348. {
  349. throw new NotImplementedException ();
  350. }
  351. void _ConstructorBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  352. {
  353. throw new NotImplementedException ();
  354. }
  355. void _ConstructorBuilder.GetTypeInfoCount (out uint pcTInfo)
  356. {
  357. throw new NotImplementedException ();
  358. }
  359. void _ConstructorBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  360. {
  361. throw new NotImplementedException ();
  362. }
  363. }
  364. }