2 // System.Reflection.Emit/ConstructorBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 using System.Reflection;
12 using System.Reflection.Emit;
13 using System.Globalization;
14 using System.Security;
15 using System.Security.Permissions;
17 namespace System.Reflection.Emit {
18 public sealed class ConstructorBuilder : ConstructorInfo {
19 private RuntimeMethodHandle mhandle;
20 private ILGenerator ilgen;
21 private Type[] parameters;
22 private MethodAttributes attrs;
23 private MethodImplAttributes iattrs;
24 private int table_idx;
25 private CallingConventions call_conv;
26 private TypeBuilder type;
27 private ParameterBuilder[] pinfo;
28 private CustomAttributeBuilder[] cattrs;
29 private bool init_locals = true;
31 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) {
32 attrs = attributes | MethodAttributes.SpecialName;
33 call_conv = callingConvention;
34 if (parameterTypes != null) {
35 this.parameters = new Type [parameterTypes.Length];
36 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
39 table_idx = get_next_table_index (this, 0x06, true);
42 public bool InitLocals {
43 get {return init_locals;}
44 set {init_locals = value;}
47 internal TypeBuilder TypeBuilder {
51 public override MethodImplAttributes GetMethodImplementationFlags() {
54 public override ParameterInfo[] GetParameters() {
55 if (parameters == null)
58 ParameterInfo[] retval = new ParameterInfo [parameters.Length];
59 for (int i = 0; i < parameters.Length; i++) {
60 retval [i] = new ParameterInfo (pinfo == null ? null : pinfo [i+1], parameters [i], this, i + 1);
65 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) {
66 throw not_supported ();
68 public override object Invoke(BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) {
69 throw not_supported ();
72 public override RuntimeMethodHandle MethodHandle {
74 throw not_supported ();
78 public override MethodAttributes Attributes {
81 public override Type ReflectedType { get {return type;}}
82 public override Type DeclaringType { get {return type;}}
83 public Type ReturnType { get {return null;}}
84 public override string Name {
85 get {return (attrs & MethodAttributes.Static) != 0 ? ".cctor" : ".ctor";}
87 public string Signature {
88 get {return "constructor signature";}
91 public void AddDeclarativeSecurity( SecurityAction action, PermissionSet pset) {
95 public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string strParamName)
97 if ((iSequence < 1) || (iSequence > parameters.Length))
98 throw new ArgumentOutOfRangeException ("iSequence");
100 throw not_after_created ();
102 ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
105 pinfo = new ParameterBuilder [parameters.Length + 1];
106 pinfo [iSequence] = pb;
110 public override bool IsDefined (Type attribute_type, bool inherit) {
111 throw not_supported ();
114 public override object [] GetCustomAttributes (bool inherit) {
115 throw not_supported ();
118 public override object [] GetCustomAttributes (Type attribute_type, bool inherit) {
119 throw not_supported ();
122 public ILGenerator GetILGenerator () {
123 return GetILGenerator (64);
125 internal ILGenerator GetILGenerator (int size) {
126 ilgen = new ILGenerator (this, size);
130 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
131 if (customBuilder == null)
132 throw new ArgumentNullException ("customBuilder");
134 string attrname = customBuilder.Ctor.ReflectedType.FullName;
135 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
136 byte[] data = customBuilder.Data;
137 int impla; // the (stupid) ctor takes a short or an int ...
138 impla = (int)data [2];
139 impla |= ((int)data [3]) << 8;
140 SetImplementationFlags ((MethodImplAttributes)impla);
143 if (cattrs != null) {
144 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
145 cattrs.CopyTo (new_array, 0);
146 new_array [cattrs.Length] = customBuilder;
149 cattrs = new CustomAttributeBuilder [1];
150 cattrs [0] = customBuilder;
153 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
155 throw new ArgumentNullException ("con");
156 if (binaryAttribute == null)
157 throw new ArgumentNullException ("binaryAttribute");
159 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
161 public void SetImplementationFlags( MethodImplAttributes attributes) {
163 throw not_after_created ();
167 public Module GetModule() {
170 public MethodToken GetToken() {
171 return new MethodToken (0x06000000 | table_idx);
175 public void SetSymCustomAttribute( string name, byte[] data) {
177 throw not_after_created ();
180 public override string ToString() {
181 return "constructor";
184 internal void fixup () {
186 ilgen.label_fixup ();
188 internal override int get_next_table_index (object obj, int table, bool inc) {
189 return type.get_next_table_index (obj, table, inc);
192 private Exception not_supported () {
193 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
196 private Exception not_after_created () {
197 return new InvalidOperationException ("Unable to change after type has been created.");