PropertyBuilder Class
Defines the properties for a type.
System.Reflection.MemberInfo
System.Reflection.PropertyInfo
System.Reflection.Emit.PropertyBuilder
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 can be read. (Overrides PropertyInfo.CanRead.) |
![]() | CanWrite | Gets a value indicating whether the property can be written to. (Overrides PropertyInfo.CanWrite.) |
![]() | DeclaringType | Gets the class that declares this member. (Overrides MemberInfo.DeclaringType.) |
![]() | IsSpecialName | Gets a value indicating whether the property is the special name. (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 | Retrieves the token for this property. |
![]() | PropertyType | Gets the type of the field of this property. (Overrides PropertyInfo.PropertyType.) |
![]() | ReflectedType | Gets the class object that was used to obtain this instance of MemberInfo. (Overrides MemberInfo.ReflectedType.) |
| Name | Description | |
|---|---|---|
![]() | AddOtherMethod | Adds one of the other methods associated with this property. |
![]() | Equals | Returns a value that indicates whether this instance is equal to a specified object. (Inherited from PropertyInfo.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it 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. (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. (Overrides MemberInfo.GetCustomAttributes(Boolean).) |
![]() | GetCustomAttributes(Type, Boolean) | Returns an array of custom attributes identified by Type. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).) |
![]() | GetCustomAttributesData | Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member. (Inherited from MemberInfo.) |
![]() | GetGetMethod | Returns the public get accessor for this property. (Inherited from PropertyInfo.) |
![]() | GetGetMethod(Boolean) | Returns the public and non-public get accessor for this property. (Overrides PropertyInfo.GetGetMethod(Boolean).) |
![]() | GetHashCode | Returns the hash code for this instance. (Inherited from PropertyInfo.) |
![]() | GetIndexParameters | Returns an array of all the index parameters for the property. (Overrides PropertyInfo.GetIndexParameters.) |
![]() | GetOptionalCustomModifiers | Returns an array of types representing the optional custom modifiers of the property. (Inherited from PropertyInfo.) |
![]() | GetRawConstantValue | Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.) |
![]() | GetRequiredCustomModifiers | Returns an array of types representing the required custom modifiers of the property. (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. (Overrides PropertyInfo.GetValue(Object, Object()).) |
![]() | GetValue(Object, BindingFlags, Binder, Object(), CultureInfo) | Gets the value of a property having the specified binding, index, and CultureInfo. (Overrides PropertyInfo.GetValue(Object, BindingFlags, Binder, Object(), CultureInfo).) |
![]() | IsDefined | Indicates whether one or more instance of attributeType is defined on this property. (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(CustomAttributeBuilder) | Set a custom attribute using a custom attribute builder. |
![]() | SetCustomAttribute(ConstructorInfo, Byte()) | Set a custom attribute using a specified custom attribute blob. |
![]() | SetGetMethod | Sets the method that gets the property value. |
![]() | SetSetMethod | Sets the method that sets the property value. |
![]() | SetValue(Object, Object, Object()) | Sets the value of the property with optional index values for index properties. (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. (Overrides PropertyInfo.SetValue(Object, Object, BindingFlags, Binder, Object(), CultureInfo).) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _MemberInfo.GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo.GetType | Gets a Type object representing the MemberInfo class. (Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo.GetTypeInfo | Retrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo.GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from MemberInfo.) |
![]() ![]() | _MemberInfo.Invoke | Provides access to properties and methods exposed by an object. (Inherited from MemberInfo.) |
![]() ![]() | _PropertyBuilder.GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. |
![]() ![]() | _PropertyBuilder.GetTypeInfo | Retrieves the type information for an object, which can then be used to get the type information for an interface. |
![]() ![]() | _PropertyBuilder.GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). |
![]() ![]() | _PropertyBuilder.Invoke | Provides access to properties and methods exposed by an object. |
![]() ![]() | _PropertyInfo.GetIDsOfNames | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from PropertyInfo.) |
![]() ![]() | _PropertyInfo.GetType | Gets a Type object representing the PropertyInfo type. (Inherited from PropertyInfo.) |
![]() ![]() | _PropertyInfo.GetTypeInfo | Retrieves the type information for an object, which can then be used to get the type information for an interface. (Inherited from PropertyInfo.) |
![]() ![]() | _PropertyInfo.GetTypeInfoCount | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from PropertyInfo.) |
![]() ![]() | _PropertyInfo.Invoke | Provides access to properties and methods exposed by an object. (Inherited from PropertyInfo.) |
A PropertyBuilder is always associated with a TypeBuilder. The TypeBuilder. DefineProperty method will return a new PropertyBuilder to a client.
Note |
|---|
The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: MayLeakOnAbort. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes. |
The following code sample demonstrates how to implement properties in a dynamic type using a PropertyBuilder obtained via TypeBuilder.DefineProperty to create the property framework and an associated MethodBuilder to implement the IL logic within the property.
Imports System Imports System.Threading Imports System.Reflection Imports System.Reflection.Emit Class PropertyBuilderDemo Public Shared Function BuildDynamicTypeWithProperties() As Type Dim myDomain As AppDomain = Thread.GetDomain() Dim myAsmName As New AssemblyName() myAsmName.Name = "MyDynamicAssembly" ' To generate a persistable assembly, specify AssemblyBuilderAccess.RunAndSave. Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _ AssemblyBuilderAccess.RunAndSave) ' Generate a persistable, single-module assembly. Dim myModBuilder As ModuleBuilder = _ myAsmBuilder.DefineDynamicModule(myAsmName.Name, myAsmName.Name & ".dll") Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public) ' Define a private field to hold the property value. Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _ GetType(String), FieldAttributes.Private) ' The last argument of DefineProperty is Nothing, because the ' property has no parameters. (If you don't specify Nothing, you must ' specify an array of Type objects. For a parameterless property, ' use an array with no elements: New Type() {}) Dim custNamePropBldr As PropertyBuilder = _ myTypeBuilder.DefineProperty("CustomerName", _ PropertyAttributes.HasDefault, _ GetType(String), _ Nothing) ' The property set and property get methods require a special ' set of attributes. Dim getSetAttr As MethodAttributes = _ MethodAttributes.Public Or MethodAttributes.SpecialName _ Or MethodAttributes.HideBySig ' Define the "get" accessor method for CustomerName. Dim custNameGetPropMthdBldr As MethodBuilder = _ myTypeBuilder.DefineMethod("GetCustomerName", _ getSetAttr, _ GetType(String), _ Type.EmptyTypes) Dim custNameGetIL As ILGenerator = custNameGetPropMthdBldr.GetILGenerator() custNameGetIL.Emit(OpCodes.Ldarg_0) custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr) custNameGetIL.Emit(OpCodes.Ret) ' Define the "set" accessor method for CustomerName. Dim custNameSetPropMthdBldr As MethodBuilder = _ myTypeBuilder.DefineMethod("get_CustomerName", _ getSetAttr, _ Nothing, _ New Type() {GetType(String)}) Dim custNameSetIL As ILGenerator = custNameSetPropMthdBldr.GetILGenerator() custNameSetIL.Emit(OpCodes.Ldarg_0) custNameSetIL.Emit(OpCodes.Ldarg_1) custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr) custNameSetIL.Emit(OpCodes.Ret) ' Last, we must map the two methods created above to our PropertyBuilder to ' their corresponding behaviors, "get" and "set" respectively. custNamePropBldr.SetGetMethod(custNameGetPropMthdBldr) custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr) Dim retval As Type = myTypeBuilder.CreateType() ' Save the assembly so it can be examined with Ildasm.exe, ' or referenced by a test program. myAsmBuilder.Save(myAsmName.Name & ".dll") return retval End Function 'BuildDynamicTypeWithProperties Public Shared Sub Main() Dim custDataType As Type = BuildDynamicTypeWithProperties() Dim custDataPropInfo As PropertyInfo() = custDataType.GetProperties() Dim pInfo As PropertyInfo For Each pInfo In custDataPropInfo Console.WriteLine("Property '{0}' created!", pInfo.ToString()) Next pInfo Console.WriteLine("---") ' Note that when invoking a property, you need to use the proper BindingFlags - ' BindingFlags.SetProperty when you invoke the "set" behavior, and ' BindingFlags.GetProperty when you invoke the "get" behavior. Also note that ' we invoke them based on the name we gave the property, as expected, and not ' the name of the methods we bound to the specific property behaviors. Dim custData As Object = Activator.CreateInstance(custDataType) custDataType.InvokeMember("CustomerName", BindingFlags.SetProperty, Nothing, _ custData, New Object() {"Joe User"}) Console.WriteLine("The customerName field of instance custData has been set to '{0}'.", _ custDataType.InvokeMember("CustomerName", BindingFlags.GetProperty, _ Nothing, custData, New Object() {})) End Sub 'Main End Class 'PropertyBuilderDemo ' --- O U T P U T --- ' The output should be as follows: ' ------------------- ' Property 'System.String CustomerName [System.String]' created! ' --- ' The customerName field of instance custData has been set to 'Joe User'. ' -------------------
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.





Note