3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
9 namespace System.Reflection.Emit
12 using System.Reflection;
13 using CultureInfo = System.Globalization.CultureInfo;
14 using System.Collections.Generic;
15 using System.Diagnostics.SymbolStore;
16 using System.Security;
17 using System.Security.Permissions;
18 using System.Runtime.InteropServices;
19 using System.Diagnostics.Contracts;
21 [HostProtection(MayLeakOnAbort = true)]
22 [ClassInterface(ClassInterfaceType.None)]
23 [ComDefaultInterface(typeof(_ConstructorBuilder))]
24 [System.Runtime.InteropServices.ComVisible(true)]
25 public sealed class ConstructorBuilder : ConstructorInfo, _ConstructorBuilder
27 private readonly MethodBuilder m_methodBuilder;
28 internal bool m_isDefaultConstructor;
32 private ConstructorBuilder()
36 [System.Security.SecurityCritical] // auto-generated
37 internal ConstructorBuilder(String name, MethodAttributes attributes, CallingConventions callingConvention,
38 Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers, ModuleBuilder mod, TypeBuilder type)
44 m_methodBuilder = new MethodBuilder(name, attributes, callingConvention, null, null, null,
45 parameterTypes, requiredCustomModifiers, optionalCustomModifiers, mod, type, false);
47 type.m_listMethods.Add(m_methodBuilder);
49 sigBytes = m_methodBuilder.GetMethodSignature().InternalGetSignature(out sigLength);
51 token = m_methodBuilder.GetToken();
54 [System.Security.SecurityCritical] // auto-generated
55 internal ConstructorBuilder(String name, MethodAttributes attributes, CallingConventions callingConvention,
56 Type[] parameterTypes, ModuleBuilder mod, TypeBuilder type) :
57 this(name, attributes, callingConvention, parameterTypes, null, null, mod, type)
64 internal override Type[] GetParameterTypes()
66 return m_methodBuilder.GetParameterTypes();
69 private TypeBuilder GetTypeBuilder()
71 return m_methodBuilder.GetTypeBuilder();
74 internal ModuleBuilder GetModuleBuilder()
76 return GetTypeBuilder().GetModuleBuilder();
80 #region Object Overrides
81 public override String ToString()
83 return m_methodBuilder.ToString();
88 #region MemberInfo Overrides
89 internal int MetadataTokenInternal
91 get { return m_methodBuilder.MetadataTokenInternal; }
94 public override Module Module
96 get { return m_methodBuilder.Module; }
99 public override Type ReflectedType
101 get { return m_methodBuilder.ReflectedType; }
104 public override Type DeclaringType
106 get { return m_methodBuilder.DeclaringType; }
109 public override String Name
111 get { return m_methodBuilder.Name; }
116 #region MethodBase Overrides
117 public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
119 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
123 public override ParameterInfo[] GetParameters()
125 ConstructorInfo rci = GetTypeBuilder().GetConstructor(m_methodBuilder.m_parameterTypes);
126 return rci.GetParameters();
129 public override MethodAttributes Attributes
131 get { return m_methodBuilder.Attributes; }
134 public override MethodImplAttributes GetMethodImplementationFlags()
136 return m_methodBuilder.GetMethodImplementationFlags();
139 public override RuntimeMethodHandle MethodHandle
141 get { return m_methodBuilder.MethodHandle; }
146 #region ConstructorInfo Overrides
147 public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
149 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
154 #region ICustomAttributeProvider Implementation
155 public override Object[] GetCustomAttributes(bool inherit)
157 return m_methodBuilder.GetCustomAttributes(inherit);
160 public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
162 return m_methodBuilder.GetCustomAttributes(attributeType, inherit);
165 public override bool IsDefined (Type attributeType, bool inherit)
167 return m_methodBuilder.IsDefined(attributeType, inherit);
172 #region Public Members
173 public MethodToken GetToken()
175 return m_methodBuilder.GetToken();
178 public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, String strParamName)
180 // Theoretically we shouldn't allow iSequence to be 0 because in reflection ctors don't have
181 // return parameters. But we'll allow it for backward compatibility with V2. The attributes
182 // defined on the return parameters won't be very useful but won't do much harm either.
184 // MD will assert if we try to set the reserved bits explicitly
185 attributes = attributes & ~ParameterAttributes.ReservedMask;
186 return m_methodBuilder.DefineParameter(iSequence, attributes, strParamName);
189 public void SetSymCustomAttribute(String name, byte[] data)
191 m_methodBuilder.SetSymCustomAttribute(name, data);
194 public ILGenerator GetILGenerator()
196 if (m_isDefaultConstructor)
197 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorILGen"));
199 return m_methodBuilder.GetILGenerator();
202 public ILGenerator GetILGenerator(int streamSize)
204 if (m_isDefaultConstructor)
205 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorILGen"));
207 return m_methodBuilder.GetILGenerator(streamSize);
211 [System.Security.SecurityCritical] // auto-generated
213 public void SetMethodBody(byte[] il, int maxStack, byte[] localSignature, IEnumerable<ExceptionHandler> exceptionHandlers, IEnumerable<int> tokenFixups)
215 if (m_isDefaultConstructor)
217 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorDefineBody"));
220 m_methodBuilder.SetMethodBody(il, maxStack, localSignature, exceptionHandlers, tokenFixups);
223 #if FEATURE_CAS_POLICY
224 [System.Security.SecuritySafeCritical] // auto-generated
225 public void AddDeclarativeSecurity(SecurityAction action, PermissionSet pset)
228 throw new ArgumentNullException("pset");
230 #pragma warning disable 618
231 if (!Enum.IsDefined(typeof(SecurityAction), action) ||
232 action == SecurityAction.RequestMinimum ||
233 action == SecurityAction.RequestOptional ||
234 action == SecurityAction.RequestRefuse)
236 throw new ArgumentOutOfRangeException("action");
238 #pragma warning restore 618
239 Contract.EndContractBlock();
241 // Cannot add declarative security after type is created.
242 if (m_methodBuilder.IsTypeCreated())
243 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_TypeHasBeenCreated"));
245 // Translate permission set into serialized format (use standard binary serialization).
246 byte[] blob = pset.EncodeXml();
248 // Write the blob into the metadata.
249 TypeBuilder.AddDeclarativeSecurity(GetModuleBuilder().GetNativeHandle(), GetToken().Token, action, blob, blob.Length);
251 #endif // FEATURE_CAS_POLICY
253 public override CallingConventions CallingConvention
257 if (DeclaringType.IsGenericType)
258 return CallingConventions.HasThis;
260 return CallingConventions.Standard;
264 public Module GetModule()
266 return m_methodBuilder.GetModule();
270 [Obsolete("This property has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")] //It always returns null.
271 public Type ReturnType
273 get { return GetReturnType(); }
276 // This always returns null. Is that what we want?
277 internal override Type GetReturnType()
279 return m_methodBuilder.ReturnType;
282 public String Signature
284 get { return m_methodBuilder.Signature; }
288 [System.Security.SecurityCritical] // auto-generated
290 [System.Runtime.InteropServices.ComVisible(true)]
291 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
293 m_methodBuilder.SetCustomAttribute(con, binaryAttribute);
296 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
298 m_methodBuilder.SetCustomAttribute(customBuilder);
301 public void SetImplementationFlags(MethodImplAttributes attributes)
303 m_methodBuilder.SetImplementationFlags(attributes);
306 public bool InitLocals
308 get { return m_methodBuilder.InitLocals; }
309 set { m_methodBuilder.InitLocals = value; }
315 void _ConstructorBuilder.GetTypeInfoCount(out uint pcTInfo)
317 throw new NotImplementedException();
320 void _ConstructorBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
322 throw new NotImplementedException();
325 void _ConstructorBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
327 throw new NotImplementedException();
330 void _ConstructorBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
332 throw new NotImplementedException();