ConstructorBuilder.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. using System;
  10. using System.Reflection;
  11. using System.Reflection.Emit;
  12. using System.Globalization;
  13. using System.Security;
  14. using System.Security.Permissions;
  15. namespace System.Reflection.Emit {
  16. public sealed class ConstructorBuilder : ConstructorInfo {
  17. private RuntimeMethodHandle mhandle;
  18. private ILGenerator ilgen;
  19. private Type[] parameters;
  20. private MethodAttributes attrs;
  21. private MethodImplAttributes iattrs;
  22. private int table_idx;
  23. private CallingConventions call_conv;
  24. private TypeBuilder type;
  25. private ParameterBuilder[] pinfo;
  26. private CustomAttributeBuilder[] cattrs;
  27. private bool init_locals = true;
  28. internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
  29. attrs = attributes;
  30. call_conv = callingConvention;
  31. if (parameterTypes != null) {
  32. this.parameters = new Type [parameterTypes.Length];
  33. System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
  34. }
  35. type = tb;
  36. table_idx = get_next_table_index (this, 0x06, true);
  37. }
  38. public bool InitLocals {
  39. get {return init_locals;}
  40. set {init_locals = value;}
  41. }
  42. internal TypeBuilder TypeBuilder {
  43. get {return type;}
  44. }
  45. public override MethodImplAttributes GetMethodImplementationFlags() {
  46. return iattrs;
  47. }
  48. public override ParameterInfo[] GetParameters() {
  49. if ((parameters == null) || (pinfo == null))
  50. return null;
  51. ParameterInfo[] retval = new ParameterInfo [parameters.Length];
  52. for (int i = 0; i < parameters.Length; i++) {
  53. if (pinfo [i+1] == null)
  54. return null;
  55. retval [i] = new ParameterInfo (pinfo [i+1], parameters [i], this);
  56. }
  57. return retval;
  58. }
  59. public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
  60. return null;
  61. }
  62. public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) {
  63. return null;
  64. }
  65. public override RuntimeMethodHandle MethodHandle { get {return new RuntimeMethodHandle ();} }
  66. public override MethodAttributes Attributes {
  67. get {return attrs;}
  68. }
  69. public override Type ReflectedType { get {return type;}}
  70. public override Type DeclaringType { get {return type;}}
  71. public Type ReturnType { get {return null;}}
  72. public override string Name {
  73. get {return (attrs & MethodAttributes.Static) != 0 ? ".cctor" : ".ctor";}
  74. }
  75. public string Signature {
  76. get {return "constructor signature";}
  77. }
  78. public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
  79. }
  80. [MonoTODO]
  81. public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName)
  82. {
  83. ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
  84. // check iSequence
  85. if (pinfo == null)
  86. pinfo = new ParameterBuilder [parameters.Length + 1];
  87. pinfo [iSequence] = pb;
  88. return pb;
  89. }
  90. public override bool IsDefined (Type attribute_type, bool inherit) {return false;}
  91. public override object [] GetCustomAttributes (bool inherit) {return null;}
  92. public override object [] GetCustomAttributes (Type attribute_type, bool inherit) {return null;}
  93. public ILGenerator GetILGenerator () {
  94. return GetILGenerator (256);
  95. }
  96. internal ILGenerator GetILGenerator (int size) {
  97. ilgen = new ILGenerator (this, size);
  98. return ilgen;
  99. }
  100. public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
  101. string attrname = customBuilder.Ctor.ReflectedType.FullName;
  102. if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
  103. byte[] data = customBuilder.Data;
  104. int impla; // the (stupid) ctor takes a short or an int ...
  105. impla = (int)data [2];
  106. impla |= ((int)data [3]) << 8;
  107. SetImplementationFlags ((MethodImplAttributes)impla);
  108. return;
  109. }
  110. if (cattrs != null) {
  111. CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
  112. cattrs.CopyTo (new_array, 0);
  113. new_array [cattrs.Length] = customBuilder;
  114. cattrs = new_array;
  115. } else {
  116. cattrs = new CustomAttributeBuilder [1];
  117. cattrs [0] = customBuilder;
  118. }
  119. }
  120. public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
  121. SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
  122. }
  123. public void SetImplementationFlags( MethodImplAttributes attributes) {
  124. iattrs = attributes;
  125. }
  126. public Module GetModule() {
  127. return null;
  128. }
  129. public MethodToken GetToken() {
  130. return new MethodToken (0x06000000 | table_idx);
  131. }
  132. public void SetSymCustomAttribute( string name, byte[] data) {
  133. }
  134. public override string ToString() {
  135. return "constructor";
  136. }
  137. internal void fixup () {
  138. if (ilgen != null)
  139. ilgen.label_fixup ();
  140. }
  141. internal override int get_next_table_index (object obj, int table, bool inc) {
  142. return type.get_next_table_index (obj, table, inc);
  143. }
  144. }
  145. }