Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / reflection / emit / propertybuilder.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  PropertyBuilder
9 ** 
10 ** <OWNER>Microsoft</OWNER>
11 **
12 **
13 ** Propertybuilder is for client to define properties for a class
14 **
15 ** 
16 ===========================================================*/
17 namespace System.Reflection.Emit {
18     
19     using System;
20     using System.Reflection;
21     using CultureInfo = System.Globalization.CultureInfo;
22     using System.Security.Permissions;
23     using System.Runtime.InteropServices;
24     using System.Diagnostics.Contracts;
25
26     // 
27     // A PropertyBuilder is always associated with a TypeBuilder.  The TypeBuilder.DefineProperty
28     // method will return a new PropertyBuilder to a client.
29     // 
30     [HostProtection(MayLeakOnAbort = true)]
31     [ClassInterface(ClassInterfaceType.None)]
32     [ComDefaultInterface(typeof(_PropertyBuilder))]
33     [System.Runtime.InteropServices.ComVisible(true)]
34     public sealed class PropertyBuilder : PropertyInfo, _PropertyBuilder
35     { 
36     
37         // Make a private constructor so these cannot be constructed externally.
38         private PropertyBuilder() {}
39         
40         // Constructs a PropertyBuilder.  
41         //
42         internal PropertyBuilder(
43             ModuleBuilder       mod,            // the module containing this PropertyBuilder
44             String              name,           // property name
45             SignatureHelper     sig,            // property signature descriptor info
46             PropertyAttributes  attr,           // property attribute such as DefaultProperty, Bindable, DisplayBind, etc
47             Type                returnType,     // return type of the property.
48             PropertyToken       prToken,        // the metadata token for this property
49             TypeBuilder         containingType) // the containing type
50         {
51             if (name == null)
52                 throw new ArgumentNullException("name");
53             if (name.Length == 0)
54                 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
55             if (name[0] == '\0')
56                 throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "name");
57             Contract.EndContractBlock();
58             
59             m_name = name;
60             m_moduleBuilder = mod;
61             m_signature = sig;
62             m_attributes = attr;
63             m_returnType = returnType;
64             m_prToken = prToken;
65             m_tkProperty = prToken.Token;
66             m_containingType = containingType;
67         }
68     
69         //************************************************
70         // Set the default value of the Property
71         //************************************************
72         [System.Security.SecuritySafeCritical]  // auto-generated
73         public void SetConstant(Object defaultValue) 
74         {
75             m_containingType.ThrowIfCreated();
76         
77             TypeBuilder.SetConstantValue(
78                 m_moduleBuilder,
79                 m_prToken.Token,
80                 m_returnType,
81                 defaultValue);
82         }
83         
84
85         // Return the Token for this property within the TypeBuilder that the
86         // property is defined within.
87         public PropertyToken PropertyToken
88         {
89             get {return m_prToken;}
90         }
91
92         internal int MetadataTokenInternal
93         {
94             get 
95             {
96                 return m_tkProperty;
97             }
98         }
99         
100         public override Module Module
101         {
102             get 
103             {
104                 return m_containingType.Module;
105             }
106         }
107
108         [System.Security.SecurityCritical]  // auto-generated
109         private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
110         {
111             if (mdBuilder == null)
112             {
113                 throw new ArgumentNullException("mdBuilder");
114             }
115
116             m_containingType.ThrowIfCreated();
117             TypeBuilder.DefineMethodSemantics(
118                 m_moduleBuilder.GetNativeHandle(),
119                 m_prToken.Token,
120                 semantics,
121                 mdBuilder.GetToken().Token);
122         }    
123
124         [System.Security.SecuritySafeCritical]  // auto-generated
125         public void SetGetMethod(MethodBuilder mdBuilder)
126         {
127             SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Getter);
128             m_getMethod = mdBuilder;
129         }
130     
131         [System.Security.SecuritySafeCritical]  // auto-generated
132         public void SetSetMethod(MethodBuilder mdBuilder)
133         {
134             SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Setter);
135             m_setMethod = mdBuilder;                
136         }
137
138         [System.Security.SecuritySafeCritical]  // auto-generated
139         public void AddOtherMethod(MethodBuilder mdBuilder)
140         {
141             SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Other);
142         }
143     
144         // Use this function if client decides to form the custom attribute blob themselves
145
146 #if FEATURE_CORECLR
147 [System.Security.SecurityCritical] // auto-generated
148 #else
149 [System.Security.SecuritySafeCritical]
150 #endif
151 [System.Runtime.InteropServices.ComVisible(true)]
152         public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
153         {
154             if (con == null)
155                 throw new ArgumentNullException("con");
156             if (binaryAttribute == null)
157                 throw new ArgumentNullException("binaryAttribute");
158             
159             m_containingType.ThrowIfCreated();
160             TypeBuilder.DefineCustomAttribute(
161                 m_moduleBuilder,
162                 m_prToken.Token,
163                 m_moduleBuilder.GetConstructorToken(con).Token,
164                 binaryAttribute,
165                 false, false);
166         }
167
168         // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
169         [System.Security.SecuritySafeCritical]  // auto-generated
170         public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
171         {
172             if (customBuilder == null)
173             {
174                 throw new ArgumentNullException("customBuilder");
175             }
176             m_containingType.ThrowIfCreated();
177             customBuilder.CreateCustomAttribute(m_moduleBuilder, m_prToken.Token);
178         }
179
180         // Not supported functions in dynamic module.
181         public override Object GetValue(Object obj,Object[] index)
182         {
183             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
184         }
185
186         public override Object GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object[] index,CultureInfo culture)
187         {
188             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
189         }
190
191         public override void SetValue(Object obj,Object value,Object[] index)
192         {
193             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
194         }
195
196         public override void SetValue(Object obj,Object value,BindingFlags invokeAttr,Binder binder,Object[] index,CultureInfo culture)
197         {
198             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
199         }
200
201         public override MethodInfo[] GetAccessors(bool nonPublic)
202         {
203             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
204         }
205
206         public override MethodInfo GetGetMethod(bool nonPublic)
207         {
208             if (nonPublic || m_getMethod == null)
209                 return m_getMethod;
210             // now check to see if m_getMethod is public
211             if ((m_getMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
212                 return m_getMethod;
213             return null;
214         }
215
216         public override MethodInfo GetSetMethod(bool nonPublic)
217         {
218             if (nonPublic || m_setMethod == null)
219                 return m_setMethod;
220             // now check to see if m_setMethod is public
221             if ((m_setMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
222                 return m_setMethod;
223             return null;
224         }
225
226         public override ParameterInfo[] GetIndexParameters()
227         {
228             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
229         }
230
231         public override Type PropertyType {
232             get { return m_returnType; }
233         }
234
235         public override PropertyAttributes Attributes {
236             get { return m_attributes;}
237         }
238
239         public override bool CanRead {
240             get { if (m_getMethod != null) return true; else return false; } }
241
242         public override bool CanWrite {
243             get { if (m_setMethod != null) return true; else return false; }
244         }
245
246         public override Object[] GetCustomAttributes(bool inherit)
247         {
248             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
249         }
250
251         public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
252         {
253             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
254         }
255
256         public override bool IsDefined (Type attributeType, bool inherit)
257         {
258             throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
259         }
260
261 #if !FEATURE_CORECLR
262         void _PropertyBuilder.GetTypeInfoCount(out uint pcTInfo)
263         {
264             throw new NotImplementedException();
265         }
266
267         void _PropertyBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
268         {
269             throw new NotImplementedException();
270         }
271
272         void _PropertyBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
273         {
274             throw new NotImplementedException();
275         }
276
277         void _PropertyBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
278         {
279             throw new NotImplementedException();
280         }
281 #endif
282
283         public override String Name {
284             get { return m_name; }
285         }
286
287         public override Type DeclaringType {
288             get { return m_containingType; }
289         }
290
291         public override Type ReflectedType {
292             get { return m_containingType; }
293         }
294
295         // These are package private so that TypeBuilder can access them.
296         private String              m_name;                // The name of the property
297         private PropertyToken       m_prToken;            // The token of this property
298         private int                 m_tkProperty;
299         private ModuleBuilder       m_moduleBuilder;
300         private SignatureHelper     m_signature;
301         private PropertyAttributes  m_attributes;        // property's attribute flags
302         private Type                m_returnType;        // property's return type
303         private MethodInfo          m_getMethod;
304         private MethodInfo          m_setMethod;
305         private TypeBuilder         m_containingType;
306     }
307
308 }