PropertyBuilder Class
Defines a property for a dynamic type.
System.Reflection.MemberInfo
System.Reflection.PropertyInfo
System.Reflection.Emit.PropertyBuilder
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
The PropertyBuilder type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Attributes | Gets the attributes for this property. (Overrides PropertyInfo.Attributes.) |
![]() | CanRead | Gets a value indicating whether the property has a get accessor. (Overrides PropertyInfo.CanRead.) |
![]() | CanWrite | Gets a value indicating whether the property has a set accessor. (Overrides PropertyInfo.CanWrite.) |
![]() | DeclaringType | Gets the class that declares this member. (Overrides MemberInfo.DeclaringType.) |
![]() | IsSpecialName | Gets a value indicating whether the name of the property is recognized as a special name by compilers. (Inherited from PropertyInfo.) |
![]() | MemberType | Gets a MemberTypes value indicating that this member is a property. (Inherited from PropertyInfo.) |
![]() | MetadataToken | Gets a value that identifies a metadata element. (Inherited from MemberInfo.) |
![]() | Module | Gets the module in which the type that declares the current property is being defined. (Overrides MemberInfo.Module.) |
![]() | Name | Gets the name of this member. (Overrides MemberInfo.Name.) |
![]() | PropertyToken | Gets the token for this property. |
![]() | PropertyType | Gets the type of the property. (Overrides PropertyInfo.PropertyType.) |
![]() | ReflectedType | Gets the dynamic type on which this property is defined. (Overrides MemberInfo.ReflectedType.) |
| Name | Description | |
|---|---|---|
![]() | AddOtherMethod | Adds one of the methods other than property accessors that can be associated with a property. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetAccessors() | Returns an array whose elements reflect the public get, set, and other accessors of the property reflected by the current instance. (Inherited from PropertyInfo.) |
![]() | GetAccessors(Boolean) | Returns an array of the public and non-public get and set accessors on this property. This method is not supported. (Overrides PropertyInfo.GetAccessors(Boolean).) |
![]() | GetConstantValue | Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.) |
![]() | GetCustomAttributes(Boolean) | Returns an array of all the custom attributes for this property. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type, Boolean) | Returns an array of custom attributes identified by Type. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).) |
![]() | GetGetMethod() | Returns the public get accessor for this property. (Inherited from PropertyInfo.) |
![]() | GetGetMethod(Boolean) | Returns the public or non-public get accessor for this property. (Overrides PropertyInfo.GetGetMethod(Boolean).) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetIndexParameters | Returns an array of all the index parameters for the property. This method is not supported. (Overrides PropertyInfo.GetIndexParameters().) |
![]() | GetRawConstantValue | Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.) |
![]() | GetSetMethod() | Returns the public set accessor for this property. (Inherited from PropertyInfo.) |
![]() | GetSetMethod(Boolean) | Returns the set accessor for this property. (Overrides PropertyInfo.GetSetMethod(Boolean).) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetValue(Object, Object[]) | Gets the value of the indexed property by calling the property's getter method. This method is not supported. (Overrides PropertyInfo.GetValue(Object, Object[]).) |
![]() | GetValue(Object, BindingFlags, Binder, Object[], CultureInfo) | Gets the value of a property that has the specified binding, index, and CultureInfo. This method is not supported. (Overrides PropertyInfo.GetValue(Object, BindingFlags, Binder, Object[], CultureInfo).) |
![]() | IsDefined | Indicates whether one or more instances of attributeType are defined on this property. This method is not supported. (Overrides MemberInfo.IsDefined(Type, Boolean).) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | SetConstant | Sets the default value of this property. |
![]() | SetCustomAttribute | Applies a custom attribute by using a custom attribute builder. |
![]() | SetGetMethod | Sets a get accessor, which is the method that gets the property value. |
![]() | SetSetMethod | Sets the set accessor, which is the method that sets the property value. |
![]() | SetValue(Object, Object, Object[]) | Sets the value of the property with optional index values for index properties. This method is not supported. (Overrides PropertyInfo.SetValue(Object, Object, Object[]).) |
![]() | SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo) | Sets the property value for the given object to the given value. This method is not supported. (Overrides PropertyInfo.SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo).) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
A PropertyBuilder is always associated with a TypeBuilder. To get a PropertyBuilder, use the TypeBuilder.DefineProperty method.
The following example demonstrates how to implement a property in a dynamic type. The example defines a property named Number that gets and sets the value of a private field named m_number. The example defines get and set accessor methods that have the proper attributes and names consistent with the conventions used by the C# and Visual Basic compilers. The example generates method bodies for the accessors, and assigns the accessors to the property.
This example is part of a larger example provided for the TypeBuilder class.
// Define a property named Number that gets and sets the private // field. // // The last argument of DefineProperty is null, because the // property has no parameters. (If you don't specify null, you must // specify an array of Type objects. For a parameterless property, // use the built-in array with no elements: Type.EmptyTypes) PropertyBuilder pbNumber = tb.DefineProperty( "Number", PropertyAttributes.HasDefault, typeof(int), null); // The property "set" and property "get" methods require a special // set of attributes. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; // Define the "get" accessor method for Number. The method returns // an integer and has no arguments. (Note that null could be // used instead of Types.EmptyTypes) MethodBuilder mbNumberGetAccessor = tb.DefineMethod( "get_Number", getSetAttr, typeof(int), Type.EmptyTypes); ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator(); // For an instance property, argument zero is the instance. Load the // instance, then load the private field and return, leaving the // field value on the stack. numberGetIL.Emit(OpCodes.Ldarg_0); numberGetIL.Emit(OpCodes.Ldfld, fbNumber); numberGetIL.Emit(OpCodes.Ret); // Define the "set" accessor method for Number, which has no return // type and takes one argument of type int (Int32). MethodBuilder mbNumberSetAccessor = tb.DefineMethod( "set_Number", getSetAttr, null, new Type[] { typeof(int) }); ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator(); // Load the instance and then the numeric argument, then store the // argument in the field. numberSetIL.Emit(OpCodes.Ldarg_0); numberSetIL.Emit(OpCodes.Ldarg_1); numberSetIL.Emit(OpCodes.Stfld, fbNumber); numberSetIL.Emit(OpCodes.Ret); // Last, map the "get" and "set" accessor methods to the // PropertyBuilder. The property is now complete. pbNumber.SetGetMethod(mbNumberGetAccessor); pbNumber.SetSetMethod(mbNumberSetAccessor);
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.


