2 // System.Reflection.Emit.ConstructorBuilder.cs
5 // Paolo Molaro (lupus@ximian.com)
7 // (C) 2001 Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Reflection;
35 using System.Reflection.Emit;
36 using System.Globalization;
37 using System.Security;
38 using System.Security.Permissions;
39 using System.Runtime.InteropServices;
40 using System.Diagnostics.SymbolStore;
42 namespace System.Reflection.Emit {
45 [ComDefaultInterface (typeof (_ConstructorBuilder))]
46 [ClassInterface (ClassInterfaceType.None)]
47 public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder {
49 #pragma warning disable 169, 414
50 private RuntimeMethodHandle mhandle;
51 private ILGenerator ilgen;
52 internal Type[] parameters;
53 private MethodAttributes attrs;
54 private MethodImplAttributes iattrs;
55 private int table_idx;
56 private CallingConventions call_conv;
57 private TypeBuilder type;
58 internal ParameterBuilder[] pinfo;
59 private CustomAttributeBuilder[] cattrs;
60 private bool init_locals = true;
61 private Type[][] paramModReq;
62 private Type[][] paramModOpt;
63 private RefEmitPermissionSet[] permissions;
64 #pragma warning restore 169, 414
66 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
68 attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
69 call_conv = callingConvention;
70 if (parameterTypes != null) {
71 for (int i = 0; i < parameterTypes.Length; ++i)
72 if (parameterTypes [i] == null)
73 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
75 this.parameters = new Type [parameterTypes.Length];
76 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
79 this.paramModReq = paramModReq;
80 this.paramModOpt = paramModOpt;
81 table_idx = get_next_table_index (this, 0x06, true);
83 ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
87 public override CallingConventions CallingConvention {
93 public bool InitLocals {
102 internal TypeBuilder TypeBuilder {
108 public override MethodImplAttributes GetMethodImplementationFlags ()
113 public override ParameterInfo[] GetParameters ()
115 if (!type.is_created)
116 throw not_created ();
118 return GetParametersInternal ();
121 internal ParameterInfo [] GetParametersInternal ()
123 if (parameters == null)
124 return new ParameterInfo [0];
126 ParameterInfo [] retval = new ParameterInfo [parameters.Length];
127 for (int i = 0; i < parameters.Length; i++)
128 retval [i] = new ParameterInfo (pinfo == null ? null
129 : pinfo [i + 1], parameters [i], this, i + 1);
134 internal override int GetParameterCount ()
136 if (parameters == null)
139 return parameters.Length;
142 internal override Type GetParameterType (int pos) {
143 return parameters [pos];
146 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
148 throw not_supported ();
151 public override object Invoke (BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
153 throw not_supported ();
156 public override RuntimeMethodHandle MethodHandle {
158 throw not_supported ();
162 public override MethodAttributes Attributes {
168 public override Type ReflectedType {
174 public override Type DeclaringType {
183 public Type ReturnType {
189 public override string Name {
191 return (attrs & MethodAttributes.Static) != 0 ? ConstructorInfo.TypeConstructorName : ConstructorInfo.ConstructorName;
195 public string Signature {
197 return "constructor signature";
201 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
205 throw new ArgumentNullException ("pset");
206 if ((action == SecurityAction.RequestMinimum) ||
207 (action == SecurityAction.RequestOptional) ||
208 (action == SecurityAction.RequestRefuse))
209 throw new ArgumentOutOfRangeException ("action", "Request* values are not permitted");
213 if (permissions != null) {
214 /* Check duplicate actions */
215 foreach (RefEmitPermissionSet set in permissions)
216 if (set.action == action)
217 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
219 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
220 permissions.CopyTo (new_array, 0);
221 permissions = new_array;
224 permissions = new RefEmitPermissionSet [1];
226 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
227 attrs |= MethodAttributes.HasSecurity;
231 public ParameterBuilder DefineParameter (int iSequence, ParameterAttributes attributes, string strParamName)
233 if (iSequence < 1 || iSequence > GetParameterCount ())
234 throw new ArgumentOutOfRangeException ("iSequence");
236 throw not_after_created ();
238 ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
240 pinfo = new ParameterBuilder [parameters.Length + 1];
241 pinfo [iSequence] = pb;
245 public override bool IsDefined (Type attributeType, bool inherit)
247 throw not_supported ();
250 public override object [] GetCustomAttributes (bool inherit)
252 throw not_supported ();
255 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
257 throw not_supported ();
260 public ILGenerator GetILGenerator ()
262 return GetILGenerator (64);
265 public ILGenerator GetILGenerator (int streamSize)
269 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), streamSize);
273 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
275 if (customBuilder == null)
276 throw new ArgumentNullException ("customBuilder");
278 string attrname = customBuilder.Ctor.ReflectedType.FullName;
279 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
280 byte[] data = customBuilder.Data;
281 int impla; // the (stupid) ctor takes a short or an int ...
282 impla = (int)data [2];
283 impla |= ((int)data [3]) << 8;
284 SetImplementationFlags ((MethodImplAttributes)impla);
287 if (cattrs != null) {
288 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
289 cattrs.CopyTo (new_array, 0);
290 new_array [cattrs.Length] = customBuilder;
293 cattrs = new CustomAttributeBuilder [1];
294 cattrs [0] = customBuilder;
299 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
302 throw new ArgumentNullException ("con");
303 if (binaryAttribute == null)
304 throw new ArgumentNullException ("binaryAttribute");
306 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
309 public void SetImplementationFlags (MethodImplAttributes attributes)
312 throw not_after_created ();
317 public Module GetModule ()
322 public MethodToken GetToken ()
324 return new MethodToken (0x06000000 | table_idx);
328 public void SetSymCustomAttribute (string name, byte[] data)
331 throw not_after_created ();
334 public override Module Module {
340 public override string ToString ()
342 return "ConstructorBuilder ['" + type.Name + "']";
345 internal void fixup ()
347 if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
348 if ((ilgen == null) || (ilgen.ILOffset == 0))
349 throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
352 ilgen.label_fixup ();
355 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
357 if (ilgen != null && ilgen.HasDebugInfo) {
358 SymbolToken token = new SymbolToken (GetToken().Token);
359 symbolWriter.OpenMethod (token);
360 symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
361 ilgen.GenerateDebugInfo (symbolWriter);
362 symbolWriter.CloseMethod ();
366 internal override int get_next_table_index (object obj, int table, bool inc)
368 return type.get_next_table_index (obj, table, inc);
371 private void RejectIfCreated ()
374 throw new InvalidOperationException ("Type definition of the method is complete.");
377 private Exception not_supported ()
379 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
382 private Exception not_after_created ()
384 return new InvalidOperationException ("Unable to change after type has been created.");
387 private Exception not_created ()
389 return new NotSupportedException ("The type is not yet created.");
392 void _ConstructorBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
394 throw new NotImplementedException ();
397 void _ConstructorBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
399 throw new NotImplementedException ();
402 void _ConstructorBuilder.GetTypeInfoCount (out uint pcTInfo)
404 throw new NotImplementedException ();
407 void _ConstructorBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
409 throw new NotImplementedException ();