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 [StructLayout (LayoutKind.Sequential)]
48 public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder {
50 #pragma warning disable 169, 414
51 private RuntimeMethodHandle mhandle;
52 private ILGenerator ilgen;
53 internal Type[] parameters;
54 private MethodAttributes attrs;
55 private MethodImplAttributes iattrs;
56 private int table_idx;
57 private CallingConventions call_conv;
58 private TypeBuilder type;
59 internal ParameterBuilder[] pinfo;
60 private CustomAttributeBuilder[] cattrs;
61 private bool init_locals = true;
62 private Type[][] paramModReq;
63 private Type[][] paramModOpt;
64 private RefEmitPermissionSet[] permissions;
65 #pragma warning restore 169, 414
67 internal ConstructorBuilder (TypeBuilder tb, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes, Type[][] paramModReq, Type[][] paramModOpt)
69 attrs = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
70 call_conv = callingConvention;
71 if (parameterTypes != null) {
72 for (int i = 0; i < parameterTypes.Length; ++i)
73 if (parameterTypes [i] == null)
74 throw new ArgumentException ("Elements of the parameterTypes array cannot be null", "parameterTypes");
76 this.parameters = new Type [parameterTypes.Length];
77 System.Array.Copy (parameterTypes, this.parameters, parameterTypes.Length);
80 this.paramModReq = paramModReq;
81 this.paramModOpt = paramModOpt;
82 table_idx = get_next_table_index (this, 0x06, true);
84 ((ModuleBuilder) tb.Module).RegisterToken (this, GetToken ().Token);
88 public override CallingConventions CallingConvention {
94 public bool InitLocals {
103 internal TypeBuilder TypeBuilder {
109 public override MethodImplAttributes GetMethodImplementationFlags ()
114 public override ParameterInfo[] GetParameters ()
116 if (!type.is_created)
117 throw not_created ();
119 return GetParametersInternal ();
122 internal ParameterInfo [] GetParametersInternal ()
124 if (parameters == null)
125 return new ParameterInfo [0];
127 ParameterInfo [] retval = new ParameterInfo [parameters.Length];
128 for (int i = 0; i < parameters.Length; i++)
129 retval [i] = new ParameterInfo (pinfo == null ? null
130 : pinfo [i + 1], parameters [i], this, i + 1);
135 internal override int GetParameterCount ()
137 if (parameters == null)
140 return parameters.Length;
143 internal override Type GetParameterType (int pos) {
144 return parameters [pos];
147 public override Object Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
149 throw not_supported ();
152 public override object Invoke (BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
154 throw not_supported ();
157 public override RuntimeMethodHandle MethodHandle {
159 throw not_supported ();
163 public override MethodAttributes Attributes {
169 public override Type ReflectedType {
175 public override Type DeclaringType {
184 public Type ReturnType {
190 public override string Name {
192 return (attrs & MethodAttributes.Static) != 0 ? ConstructorInfo.TypeConstructorName : ConstructorInfo.ConstructorName;
196 public string Signature {
198 return "constructor signature";
202 public void AddDeclarativeSecurity (SecurityAction action, PermissionSet pset)
206 throw new ArgumentNullException ("pset");
207 if ((action == SecurityAction.RequestMinimum) ||
208 (action == SecurityAction.RequestOptional) ||
209 (action == SecurityAction.RequestRefuse))
210 throw new ArgumentOutOfRangeException ("action", "Request* values are not permitted");
214 if (permissions != null) {
215 /* Check duplicate actions */
216 foreach (RefEmitPermissionSet set in permissions)
217 if (set.action == action)
218 throw new InvalidOperationException ("Multiple permission sets specified with the same SecurityAction.");
220 RefEmitPermissionSet[] new_array = new RefEmitPermissionSet [permissions.Length + 1];
221 permissions.CopyTo (new_array, 0);
222 permissions = new_array;
225 permissions = new RefEmitPermissionSet [1];
227 permissions [permissions.Length - 1] = new RefEmitPermissionSet (action, pset.ToXml ().ToString ());
228 attrs |= MethodAttributes.HasSecurity;
232 public ParameterBuilder DefineParameter (int iSequence, ParameterAttributes attributes, string strParamName)
234 if (iSequence < 1 || iSequence > GetParameterCount ())
235 throw new ArgumentOutOfRangeException ("iSequence");
237 throw not_after_created ();
239 ParameterBuilder pb = new ParameterBuilder (this, iSequence, attributes, strParamName);
241 pinfo = new ParameterBuilder [parameters.Length + 1];
242 pinfo [iSequence] = pb;
246 public override bool IsDefined (Type attributeType, bool inherit)
248 throw not_supported ();
251 public override object [] GetCustomAttributes (bool inherit)
253 throw not_supported ();
256 public override object [] GetCustomAttributes (Type attributeType, bool inherit)
258 throw not_supported ();
261 public ILGenerator GetILGenerator ()
263 return GetILGenerator (64);
266 public ILGenerator GetILGenerator (int streamSize)
270 ilgen = new ILGenerator (type.Module, ((ModuleBuilder)type.Module).GetTokenGenerator (), streamSize);
274 public void SetCustomAttribute (CustomAttributeBuilder customBuilder)
276 if (customBuilder == null)
277 throw new ArgumentNullException ("customBuilder");
279 string attrname = customBuilder.Ctor.ReflectedType.FullName;
280 if (attrname == "System.Runtime.CompilerServices.MethodImplAttribute") {
281 byte[] data = customBuilder.Data;
282 int impla; // the (stupid) ctor takes a short or an int ...
283 impla = (int)data [2];
284 impla |= ((int)data [3]) << 8;
285 SetImplementationFlags ((MethodImplAttributes)impla);
288 if (cattrs != null) {
289 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
290 cattrs.CopyTo (new_array, 0);
291 new_array [cattrs.Length] = customBuilder;
294 cattrs = new CustomAttributeBuilder [1];
295 cattrs [0] = customBuilder;
300 public void SetCustomAttribute (ConstructorInfo con, byte[] binaryAttribute)
303 throw new ArgumentNullException ("con");
304 if (binaryAttribute == null)
305 throw new ArgumentNullException ("binaryAttribute");
307 SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
310 public void SetImplementationFlags (MethodImplAttributes attributes)
313 throw not_after_created ();
318 public Module GetModule ()
323 public MethodToken GetToken ()
325 return new MethodToken (0x06000000 | table_idx);
329 public void SetSymCustomAttribute (string name, byte[] data)
332 throw not_after_created ();
335 public override Module Module {
341 public override string ToString ()
343 return "ConstructorBuilder ['" + type.Name + "']";
346 internal void fixup ()
348 if (((attrs & (MethodAttributes.Abstract | MethodAttributes.PinvokeImpl)) == 0) && ((iattrs & (MethodImplAttributes.Runtime | MethodImplAttributes.InternalCall)) == 0)) {
349 if ((ilgen == null) || (ilgen.ILOffset == 0))
350 throw new InvalidOperationException ("Method '" + Name + "' does not have a method body.");
353 ilgen.label_fixup ();
356 internal void GenerateDebugInfo (ISymbolWriter symbolWriter)
358 if (ilgen != null && ilgen.HasDebugInfo) {
359 SymbolToken token = new SymbolToken (GetToken().Token);
360 symbolWriter.OpenMethod (token);
361 symbolWriter.SetSymAttribute (token, "__name", System.Text.Encoding.UTF8.GetBytes (Name));
362 ilgen.GenerateDebugInfo (symbolWriter);
363 symbolWriter.CloseMethod ();
367 internal override int get_next_table_index (object obj, int table, bool inc)
369 return type.get_next_table_index (obj, table, inc);
372 private void RejectIfCreated ()
375 throw new InvalidOperationException ("Type definition of the method is complete.");
378 private Exception not_supported ()
380 return new NotSupportedException ("The invoked member is not supported in a dynamic module.");
383 private Exception not_after_created ()
385 return new InvalidOperationException ("Unable to change after type has been created.");
388 private Exception not_created ()
390 return new NotSupportedException ("The type is not yet created.");
393 void _ConstructorBuilder.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
395 throw new NotImplementedException ();
398 void _ConstructorBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
400 throw new NotImplementedException ();
403 void _ConstructorBuilder.GetTypeInfoCount (out uint pcTInfo)
405 throw new NotImplementedException ();
408 void _ConstructorBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
410 throw new NotImplementedException ();