ec850ba4d40f582c62d9a3bc7ae602734ad8f4e9
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / emit / constructorbuilder.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 // 
8
9 namespace System.Reflection.Emit 
10 {    
11     using System;
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;
20     
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
26     { 
27         private readonly MethodBuilder m_methodBuilder;
28         internal bool m_isDefaultConstructor;
29
30         #region Constructor
31
32         private ConstructorBuilder()
33         {
34         }
35         
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)
39         {
40             int sigLength;
41             byte[] sigBytes;
42             MethodToken token;
43
44             m_methodBuilder = new MethodBuilder(name, attributes, callingConvention, null, null, null, 
45                 parameterTypes, requiredCustomModifiers, optionalCustomModifiers, mod, type, false);
46
47             type.m_listMethods.Add(m_methodBuilder);
48             
49             sigBytes = m_methodBuilder.GetMethodSignature().InternalGetSignature(out sigLength);
50     
51             token = m_methodBuilder.GetToken();
52         }
53
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)
58         {
59         }
60
61         #endregion
62
63         #region Internal
64         internal override Type[] GetParameterTypes()
65         {
66             return m_methodBuilder.GetParameterTypes();
67         }
68
69         private TypeBuilder GetTypeBuilder()
70         {
71             return m_methodBuilder.GetTypeBuilder();
72         }
73
74         internal ModuleBuilder GetModuleBuilder()
75         {
76             return GetTypeBuilder().GetModuleBuilder();
77         }
78         #endregion
79
80         #region Object Overrides
81         public override String ToString()
82         {
83             return m_methodBuilder.ToString();
84         }
85         
86         #endregion
87
88         #region MemberInfo Overrides
89         internal int MetadataTokenInternal
90         {
91             get { return m_methodBuilder.MetadataTokenInternal; }
92         }
93         
94         public override Module Module
95         {
96             get { return m_methodBuilder.Module; }
97         }
98         
99         public override Type ReflectedType
100         {
101             get { return m_methodBuilder.ReflectedType; }
102         }
103
104         public override Type DeclaringType
105         {
106             get { return m_methodBuilder.DeclaringType; }
107         }
108     
109         public override String Name 
110         {
111             get { return m_methodBuilder.Name; }
112         }
113
114         #endregion
115
116         #region MethodBase Overrides
117         public override Object Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
118         {
119             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule")); 
120         }
121
122         [Pure]
123         public override ParameterInfo[] GetParameters()
124         {
125             ConstructorInfo rci = GetTypeBuilder().GetConstructor(m_methodBuilder.m_parameterTypes);
126             return rci.GetParameters();
127         }
128                     
129         public override MethodAttributes Attributes
130         {
131             get { return m_methodBuilder.Attributes; }
132         }
133
134         public override MethodImplAttributes GetMethodImplementationFlags()
135         {
136             return m_methodBuilder.GetMethodImplementationFlags();
137         }
138         
139         public override RuntimeMethodHandle MethodHandle 
140         {
141             get { return m_methodBuilder.MethodHandle; }
142         }
143         
144         #endregion
145
146         #region ConstructorInfo Overrides
147         public override Object Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
148         {
149             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule")); 
150         }
151     
152         #endregion
153
154         #region ICustomAttributeProvider Implementation
155         public override Object[] GetCustomAttributes(bool inherit)
156         {
157             return m_methodBuilder.GetCustomAttributes(inherit);
158         }
159
160         public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
161         {
162             return m_methodBuilder.GetCustomAttributes(attributeType, inherit);
163         }
164         
165         public override bool IsDefined (Type attributeType, bool inherit)
166         {
167             return m_methodBuilder.IsDefined(attributeType, inherit);
168         }
169
170         #endregion
171
172         #region Public Members
173         public MethodToken GetToken()
174         {
175             return m_methodBuilder.GetToken();
176         }
177     
178         public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, String strParamName)
179         {
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.
183
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);
187         }
188     
189         public void SetSymCustomAttribute(String name, byte[] data)
190         {
191             m_methodBuilder.SetSymCustomAttribute(name, data);
192         }
193     
194         public ILGenerator GetILGenerator() 
195         {
196             if (m_isDefaultConstructor)
197                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorILGen"));
198
199             return m_methodBuilder.GetILGenerator();
200         }
201         
202         public ILGenerator GetILGenerator(int streamSize)
203         {
204             if (m_isDefaultConstructor)
205                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorILGen"));
206
207             return m_methodBuilder.GetILGenerator(streamSize);
208         }
209
210         #if FEATURE_CORECLR
211         [System.Security.SecurityCritical] // auto-generated
212         #endif
213         public void SetMethodBody(byte[] il, int maxStack, byte[] localSignature, IEnumerable<ExceptionHandler> exceptionHandlers, IEnumerable<int> tokenFixups)
214         {
215             if (m_isDefaultConstructor)
216             {
217                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_DefaultConstructorDefineBody"));
218             }
219
220             m_methodBuilder.SetMethodBody(il, maxStack, localSignature, exceptionHandlers, tokenFixups);
221         }
222
223 #if FEATURE_CAS_POLICY
224         [System.Security.SecuritySafeCritical]  // auto-generated
225         public void AddDeclarativeSecurity(SecurityAction action, PermissionSet pset)
226         {
227             if (pset == null)
228                 throw new ArgumentNullException("pset");
229
230 #pragma warning disable 618
231             if (!Enum.IsDefined(typeof(SecurityAction), action) ||
232                 action == SecurityAction.RequestMinimum ||
233                 action == SecurityAction.RequestOptional ||
234                 action == SecurityAction.RequestRefuse)
235             {
236                 throw new ArgumentOutOfRangeException("action");
237             }
238 #pragma warning restore 618
239             Contract.EndContractBlock();
240
241             // Cannot add declarative security after type is created.
242             if (m_methodBuilder.IsTypeCreated())
243                 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_TypeHasBeenCreated"));
244     
245             // Translate permission set into serialized format (use standard binary serialization).
246             byte[] blob = pset.EncodeXml();
247     
248             // Write the blob into the metadata.
249             TypeBuilder.AddDeclarativeSecurity(GetModuleBuilder().GetNativeHandle(), GetToken().Token, action, blob, blob.Length);
250         }
251 #endif // FEATURE_CAS_POLICY
252
253         public override CallingConventions CallingConvention 
254         { 
255             get 
256             { 
257                 if (DeclaringType.IsGenericType)
258                     return CallingConventions.HasThis;
259                     
260                 return CallingConventions.Standard; 
261             } 
262         }
263     
264         public Module GetModule()
265         {
266             return m_methodBuilder.GetModule();
267         }
268     
269     
270         [Obsolete("This property has been deprecated. http://go.microsoft.com/fwlink/?linkid=14202")] //It always returns null.
271         public Type ReturnType
272         {
273             get { return GetReturnType(); }
274         }
275         
276         // This always returns null. Is that what we want?
277         internal override Type GetReturnType() 
278         {
279             return m_methodBuilder.ReturnType;
280         }
281                                 
282         public String Signature 
283         {
284             get { return m_methodBuilder.Signature; }
285         }
286     
287         #if FEATURE_CORECLR
288         [System.Security.SecurityCritical] // auto-generated
289         #endif
290         [System.Runtime.InteropServices.ComVisible(true)]
291         public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
292         {
293             m_methodBuilder.SetCustomAttribute(con, binaryAttribute);
294         }
295
296         public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
297         {
298             m_methodBuilder.SetCustomAttribute(customBuilder);
299         }
300
301         public void SetImplementationFlags(MethodImplAttributes attributes) 
302         {
303             m_methodBuilder.SetImplementationFlags(attributes);
304         }
305                 
306         public bool InitLocals 
307         {
308             get { return m_methodBuilder.InitLocals; }
309             set { m_methodBuilder.InitLocals = value; }
310         }
311
312         #endregion
313
314 #if !FEATURE_CORECLR
315         void _ConstructorBuilder.GetTypeInfoCount(out uint pcTInfo)
316         {
317             throw new NotImplementedException();
318         }
319
320         void _ConstructorBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
321         {
322             throw new NotImplementedException();
323         }
324
325         void _ConstructorBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
326         {
327             throw new NotImplementedException();
328         }
329
330         void _ConstructorBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
331         {
332             throw new NotImplementedException();
333         }
334 #endif
335     }
336 }
337