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.
35 using System.Reflection;
36 using System.Reflection.Emit;
37 using System.Globalization;
38 using System.Security;
39 using System.Security.Permissions;
40 using System.Runtime.InteropServices;
41 using System.Diagnostics.SymbolStore;
43 namespace System.Reflection.Emit {
46 [ComDefaultInterface (typeof (_ConstructorBuilder))]
47 [ClassInterface (ClassInterfaceType.None)]
48 [StructLayout (LayoutKind.Sequential)]
49 public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder {
51 #pragma warning disable 169, 414
52 private RuntimeMethodHandle mhandle;
53 private ILGenerator ilgen;
54 internal Type[] parameters;
55 private MethodAttributes attrs;
56 private MethodImplAttributes iattrs;
57 private int table_idx;
58 private CallingConventions call_conv;
59 private TypeBuilder type;
60 internal ParameterBuilder[] pinfo;
61 private CustomAttributeBuilder[] cattrs;
62 private bool init_locals = true;
63 private Type[][] paramModReq;
64 private Type[][] paramModOpt;
65 private RefEmitPermissionSet[] permissions;
66 #pragma warning restore 169, 414
68 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
70 attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
71 call_conv = callingConvention;
72 if (parameterTypes != null) {
73 for (int i = 0; i < parameterTypes.Length; ++i)
74 if (parameterTypes [i] == null)
75 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
77 this.parameters = new Type [parameterTypes.Length];
78 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
81 this.paramModReq = paramModReq;
82 this.paramModOpt = paramModOpt;
83 table_idx = get_next_table_index (this, 0x06, true);
85 ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
89 public override CallingConventions CallingConvention {
95 public bool InitLocals {
104 internal TypeBuilder TypeBuilder {
110 public override MethodImplAttributes GetMethodImplementationFlags ()
115 public override ParameterInfo[] GetParameters ()
117 if (!type.is_created)
118 throw not_created ();
120 return GetParametersInternal ();
123 internal override ParameterInfo [] GetParametersInternal ()
125 if (parameters == null)
126 return EmptyArray<ParameterInfo>.Value;
128 ParameterInfo [] retval = new ParameterInfo [parameters.Length];
129 for (int i = 0; i < parameters.Length; i++)
130 retval [i] = ParameterInfo.New (pinfo == null ? null
131 : pinfo [i + 1], parameters [i], this, i + 1);
136 internal override int GetParametersCount ()
138 if (parameters == null)
141 return parameters.Length;
144 internal override Type GetParameterType (int pos) {
145 return parameters [pos];
148 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
150 throw not_supported ();
153 public override object Invoke (BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
155 throw not_supported ();
158 public override RuntimeMethodHandle MethodHandle {
160 throw not_supported ();
164 public override MethodAttributes Attributes {
170 public override Type ReflectedType {
176 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 > GetParametersCount ())
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 (this);
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 ();