PropertyBuilder Class

 

Defines the properties for a type.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

System::Object
  System.Reflection::MemberInfo
    System.Reflection::PropertyInfo
      System.Reflection.Emit::PropertyBuilder

[ClassInterfaceAttribute(ClassInterfaceType::None)]
[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction::LinkDemand, MayLeakOnAbort = true)]
public ref class PropertyBuilder sealed : PropertyInfo, _PropertyBuilder

NameDescription
System_CAPS_pubpropertyAttributes

Gets the attributes for this property.(Overrides PropertyInfo::Attributes.)

System_CAPS_pubpropertyCanRead

Gets a value indicating whether the property can be read.(Overrides PropertyInfo::CanRead.)

System_CAPS_pubpropertyCanWrite

Gets a value indicating whether the property can be written to.(Overrides PropertyInfo::CanWrite.)

System_CAPS_pubpropertyCustomAttributes

Gets a collection that contains this member's custom attributes.(Inherited from MemberInfo.)

System_CAPS_pubpropertyDeclaringType

Gets the class that declares this member.(Overrides MemberInfo::DeclaringType.)

System_CAPS_pubpropertyGetMethod

Gets the get accessor for this property.(Inherited from PropertyInfo.)

System_CAPS_pubpropertyIsSpecialName

Gets a value indicating whether the property is the special name.(Inherited from PropertyInfo.)

System_CAPS_pubpropertyMemberType

Gets a MemberTypes value indicating that this member is a property.(Inherited from PropertyInfo.)

System_CAPS_pubpropertyMetadataToken

Gets a value that identifies a metadata element.(Inherited from MemberInfo.)

System_CAPS_pubpropertyModule

Gets the module in which the type that declares the current property is being defined.(Overrides MemberInfo::Module.)

System_CAPS_pubpropertyName

Gets the name of this member.(Overrides MemberInfo::Name.)

System_CAPS_pubpropertyPropertyToken

Retrieves the token for this property.

System_CAPS_pubpropertyPropertyType

Gets the type of the field of this property.(Overrides PropertyInfo::PropertyType.)

System_CAPS_pubpropertyReflectedType

Gets the class object that was used to obtain this instance of MemberInfo.(Overrides MemberInfo::ReflectedType.)

System_CAPS_pubpropertySetMethod

Gets the set accessor for this property.(Inherited from PropertyInfo.)

NameDescription
System_CAPS_pubmethodAddOtherMethod(MethodBuilder^)

Adds one of the other methods associated with this property.

System_CAPS_pubmethodEquals(Object^)

Returns a value that indicates whether this instance is equal to a specified object.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetAccessors()

Returns an array whose elements reflect the public get and set accessors of the property reflected by the current instance. (Inherited from PropertyInfo.)

System_CAPS_pubmethodGetAccessors(Boolean)

Returns an array of the public and non-public get and set accessors on this property.(Overrides PropertyInfo::GetAccessors(Boolean).)

System_CAPS_pubmethodGetConstantValue()

Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)

System_CAPS_pubmethodGetCustomAttributes(Boolean)

Returns an array of all the custom attributes for this property.(Overrides MemberInfo::GetCustomAttributes(Boolean).)

System_CAPS_pubmethodGetCustomAttributes(Type^, Boolean)

Returns an array of custom attributes identified by Type.(Overrides MemberInfo::GetCustomAttributes(Type^, Boolean).)

System_CAPS_pubmethodGetCustomAttributesData()

Returns a list of CustomAttributeData objects representing data about the attributes that have been applied to the target member.(Inherited from MemberInfo.)

System_CAPS_pubmethodGetGetMethod()

Returns the public get accessor for this property.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetGetMethod(Boolean)

Returns the public and non-public get accessor for this property.(Overrides PropertyInfo::GetGetMethod(Boolean).)

System_CAPS_pubmethodGetHashCode()

Returns the hash code for this instance.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetIndexParameters()

Returns an array of all the index parameters for the property.(Overrides PropertyInfo::GetIndexParameters().)

System_CAPS_pubmethodGetOptionalCustomModifiers()

Returns an array of types representing the optional custom modifiers of the property.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetRawConstantValue()

Returns a literal value associated with the property by a compiler. (Inherited from PropertyInfo.)

System_CAPS_pubmethodGetRequiredCustomModifiers()

Returns an array of types representing the required custom modifiers of the property.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetSetMethod()

Returns the public set accessor for this property.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetSetMethod(Boolean)

Returns the set accessor for this property.(Overrides PropertyInfo::GetSetMethod(Boolean).)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodGetValue(Object^)

Returns the property value of a specified object.(Inherited from PropertyInfo.)

System_CAPS_pubmethodGetValue(Object^, BindingFlags, Binder^, array<Object^>^, CultureInfo^)

Gets the value of a property having the specified binding, index, and CultureInfo.(Overrides PropertyInfo::GetValue(Object^, BindingFlags, Binder^, array<Object^>^, CultureInfo^).)

System_CAPS_pubmethodGetValue(Object^, array<Object^>^)

Gets the value of the indexed property by calling the property's getter method.(Overrides PropertyInfo::GetValue(Object^, array<Object^>^).)

System_CAPS_pubmethodIsDefined(Type^, Boolean)

Indicates whether one or more instance of attributeType is defined on this property.(Overrides MemberInfo::IsDefined(Type^, Boolean).)

System_CAPS_pubmethodSetConstant(Object^)

Sets the default value of this property.

System_CAPS_pubmethodSetCustomAttribute(ConstructorInfo^, array<Byte>^)

Set a custom attribute using a specified custom attribute blob.

System_CAPS_pubmethodSetCustomAttribute(CustomAttributeBuilder^)

Set a custom attribute using a custom attribute builder.

System_CAPS_pubmethodSetGetMethod(MethodBuilder^)

Sets the method that gets the property value.

System_CAPS_pubmethodSetSetMethod(MethodBuilder^)

Sets the method that sets the property value.

System_CAPS_pubmethodSetValue(Object^, Object^)

Sets the property value of a specified object.(Inherited from PropertyInfo.)

System_CAPS_pubmethodSetValue(Object^, Object^, BindingFlags, Binder^, array<Object^>^, CultureInfo^)

Sets the property value for the given object to the given value.(Overrides PropertyInfo::SetValue(Object^, Object^, BindingFlags, Binder^, array<Object^>^, CultureInfo^).)

System_CAPS_pubmethodSetValue(Object^, Object^, array<Object^>^)

Sets the value of the property with optional index values for index properties.(Overrides PropertyInfo::SetValue(Object^, Object^, array<Object^>^).)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo::GetType()

Gets a Type object representing the MemberInfo class.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo::GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo::GetTypeInfoCount(UInt32%)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_MemberInfo::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from MemberInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyBuilder::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyBuilder::GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyBuilder::GetTypeInfoCount(UInt32%)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyBuilder::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyInfo::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from PropertyInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyInfo::GetType()

Gets a Type object representing the PropertyInfo type.(Inherited from PropertyInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyInfo::GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.(Inherited from PropertyInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyInfo::GetTypeInfoCount(UInt32%)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from PropertyInfo.)

System_CAPS_pubinterfaceSystem_CAPS_privmethod_PropertyInfo::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.(Inherited from PropertyInfo.)

NameDescription
System_CAPS_pubmethodGetCustomAttribute(Type^)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute(Type^, Boolean)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute<T>()

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttribute<T>(Boolean)

Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes()

Overloaded. Retrieves a collection of custom attributes that are applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes(Type^)

Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodGetCustomAttributes<T>(Boolean)

Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified member, and optionally inspects the ancestors of that member.(Defined by CustomAttributeExtensions.)

System_CAPS_pubmethodIsDefined(Type^)

Overloaded. Indicates whether custom attributes of a specified type are applied to a specified member.(Defined by CustomAttributeExtensions.)

A PropertyBuilder is always associated with a TypeBuilder. The TypeBuilder. DefineProperty method will return a new PropertyBuilder to a client.

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.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ BuildDynamicTypeWithProperties()
{
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew AssemblyName;
   myAsmName->Name = "MyDynamicAssembly";

   // To generate a persistable assembly, specify AssemblyBuilderAccess::RunAndSave.
   AssemblyBuilder^ myAsmBuilder = 
       myDomain->DefineDynamicAssembly( myAsmName, AssemblyBuilderAccess::RunAndSave );

   // Generate a persistable single-module assembly.
   ModuleBuilder^ myModBuilder = 
       myAsmBuilder->DefineDynamicModule( myAsmName->Name, myAsmName->Name + ".dll" );
   TypeBuilder^ myTypeBuilder = myModBuilder->DefineType( "CustomerData", TypeAttributes::Public );

   // Define a private field to hold the property value.
   FieldBuilder^ customerNameBldr = myTypeBuilder->DefineField( "customerName", String::typeid, FieldAttributes::Private );

   // The last argument of DefineProperty is an empty array of Type
   // objects, because the property has no parameters. (Alternatively,
   // you can specify a null value.)
   PropertyBuilder^ custNamePropBldr = 
       myTypeBuilder->DefineProperty( "CustomerName", PropertyAttributes::HasDefault, String::typeid, gcnew array<Type^>(0) );

   // 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 CustomerName.
   MethodBuilder^ custNameGetPropMthdBldr = 
       myTypeBuilder->DefineMethod( "get_CustomerName", 
                                    getSetAttr,
                                    String::typeid, 
                                    Type::EmptyTypes );

   ILGenerator^ custNameGetIL = custNameGetPropMthdBldr->GetILGenerator();
   custNameGetIL->Emit( OpCodes::Ldarg_0 );
   custNameGetIL->Emit( OpCodes::Ldfld, customerNameBldr );
   custNameGetIL->Emit( OpCodes::Ret );

   // Define the "set" accessor method for CustomerName.
   array<Type^>^temp2 = {String::typeid};
   MethodBuilder^ custNameSetPropMthdBldr = 
       myTypeBuilder->DefineMethod( "set_CustomerName", 
                                    getSetAttr,
                                    nullptr, 
                                    temp2 );

   ILGenerator^ custNameSetIL = 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 );

   Type^ retval = 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;
}

int main()
{
   Type^ custDataType = BuildDynamicTypeWithProperties();
   array<PropertyInfo^>^custDataPropInfo = custDataType->GetProperties();
   System::Collections::IEnumerator^ myEnum = custDataPropInfo->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      PropertyInfo^ pInfo = safe_cast<PropertyInfo^>(myEnum->Current);
      Console::WriteLine( "Property '{0}' created!", 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.
   Object^ custData = Activator::CreateInstance( custDataType );
   array<Object^>^temp3 = {"Joe User"};
   custDataType->InvokeMember( "CustomerName", BindingFlags::SetProperty, nullptr, custData, temp3 );
   Console::WriteLine( "The customerName field of instance custData has been set to '{0}'.", custDataType->InvokeMember( "CustomerName", BindingFlags::GetProperty, nullptr, custData, gcnew array<Object^>(0) ) );
}

// --- 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'.
// -------------------

.NET Framework
Available since 1.1
Silverlight
Available since 2.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: