3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: PropertyBuilder
10 ** <OWNER>Microsoft</OWNER>
13 ** Propertybuilder is for client to define properties for a class
16 ===========================================================*/
17 namespace System.Reflection.Emit {
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;
27 // A PropertyBuilder is always associated with a TypeBuilder. The TypeBuilder.DefineProperty
28 // method will return a new PropertyBuilder to a client.
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
37 // Make a private constructor so these cannot be constructed externally.
38 private PropertyBuilder() {}
40 // Constructs a PropertyBuilder.
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
52 throw new ArgumentNullException("name");
54 throw new ArgumentException(Environment.GetResourceString("Argument_EmptyName"), "name");
56 throw new ArgumentException(Environment.GetResourceString("Argument_IllegalName"), "name");
57 Contract.EndContractBlock();
60 m_moduleBuilder = mod;
63 m_returnType = returnType;
65 m_tkProperty = prToken.Token;
66 m_containingType = containingType;
69 //************************************************
70 // Set the default value of the Property
71 //************************************************
72 [System.Security.SecuritySafeCritical] // auto-generated
73 public void SetConstant(Object defaultValue)
75 m_containingType.ThrowIfCreated();
77 TypeBuilder.SetConstantValue(
85 // Return the Token for this property within the TypeBuilder that the
86 // property is defined within.
87 public PropertyToken PropertyToken
89 get {return m_prToken;}
92 internal int MetadataTokenInternal
100 public override Module Module
104 return m_containingType.Module;
108 [System.Security.SecurityCritical] // auto-generated
109 private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
111 if (mdBuilder == null)
113 throw new ArgumentNullException("mdBuilder");
116 m_containingType.ThrowIfCreated();
117 TypeBuilder.DefineMethodSemantics(
118 m_moduleBuilder.GetNativeHandle(),
121 mdBuilder.GetToken().Token);
124 [System.Security.SecuritySafeCritical] // auto-generated
125 public void SetGetMethod(MethodBuilder mdBuilder)
127 SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Getter);
128 m_getMethod = mdBuilder;
131 [System.Security.SecuritySafeCritical] // auto-generated
132 public void SetSetMethod(MethodBuilder mdBuilder)
134 SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Setter);
135 m_setMethod = mdBuilder;
138 [System.Security.SecuritySafeCritical] // auto-generated
139 public void AddOtherMethod(MethodBuilder mdBuilder)
141 SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Other);
144 // Use this function if client decides to form the custom attribute blob themselves
147 [System.Security.SecurityCritical] // auto-generated
149 [System.Security.SecuritySafeCritical]
151 [System.Runtime.InteropServices.ComVisible(true)]
152 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
155 throw new ArgumentNullException("con");
156 if (binaryAttribute == null)
157 throw new ArgumentNullException("binaryAttribute");
159 m_containingType.ThrowIfCreated();
160 TypeBuilder.DefineCustomAttribute(
163 m_moduleBuilder.GetConstructorToken(con).Token,
168 // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
169 [System.Security.SecuritySafeCritical] // auto-generated
170 public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
172 if (customBuilder == null)
174 throw new ArgumentNullException("customBuilder");
176 m_containingType.ThrowIfCreated();
177 customBuilder.CreateCustomAttribute(m_moduleBuilder, m_prToken.Token);
180 // Not supported functions in dynamic module.
181 public override Object GetValue(Object obj,Object[] index)
183 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
186 public override Object GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object[] index,CultureInfo culture)
188 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
191 public override void SetValue(Object obj,Object value,Object[] index)
193 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
196 public override void SetValue(Object obj,Object value,BindingFlags invokeAttr,Binder binder,Object[] index,CultureInfo culture)
198 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
201 public override MethodInfo[] GetAccessors(bool nonPublic)
203 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
206 public override MethodInfo GetGetMethod(bool nonPublic)
208 if (nonPublic || m_getMethod == null)
210 // now check to see if m_getMethod is public
211 if ((m_getMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
216 public override MethodInfo GetSetMethod(bool nonPublic)
218 if (nonPublic || m_setMethod == null)
220 // now check to see if m_setMethod is public
221 if ((m_setMethod.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
226 public override ParameterInfo[] GetIndexParameters()
228 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
231 public override Type PropertyType {
232 get { return m_returnType; }
235 public override PropertyAttributes Attributes {
236 get { return m_attributes;}
239 public override bool CanRead {
240 get { if (m_getMethod != null) return true; else return false; } }
242 public override bool CanWrite {
243 get { if (m_setMethod != null) return true; else return false; }
246 public override Object[] GetCustomAttributes(bool inherit)
248 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
251 public override Object[] GetCustomAttributes(Type attributeType, bool inherit)
253 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
256 public override bool IsDefined (Type attributeType, bool inherit)
258 throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicModule"));
262 void _PropertyBuilder.GetTypeInfoCount(out uint pcTInfo)
264 throw new NotImplementedException();
267 void _PropertyBuilder.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
269 throw new NotImplementedException();
272 void _PropertyBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
274 throw new NotImplementedException();
277 void _PropertyBuilder.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
279 throw new NotImplementedException();
283 public override String Name {
284 get { return m_name; }
287 public override Type DeclaringType {
288 get { return m_containingType; }
291 public override Type ReflectedType {
292 get { return m_containingType; }
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;