This documentation is archived and is not being maintained.

PropertyBuilder Class

Defines the properties for a type.

For a list of all members of this type, see PropertyBuilder Members.

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

[Visual Basic]
NotInheritable Public Class PropertyBuilder
   Inherits PropertyInfo
[C#]
public sealed class PropertyBuilder : PropertyInfo
[C++]
public __gc __sealed class PropertyBuilder : public PropertyInfo
[JScript]
public class PropertyBuilder extends PropertyInfo

Thread Safety

Reflection Emit is thread-safe when using assemblies that were created with the AppDomain.DefineDynamicAssembly method with the Boolean parameter isSynchronized set to true.

Remarks

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

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
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"
      
      Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
                            AssemblyBuilderAccess.Run)
      
      Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("MyModule")
      
      Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public)
      
      Dim customerNameBldr As FieldBuilder = myTypeBuilder.DefineField("customerName", _
                         GetType(String), FieldAttributes.Private)
      
      Dim custNamePropBldr As PropertyBuilder = myTypeBuilder.DefineProperty("CustomerName", _
                        PropertyAttributes.HasDefault, _
                        GetType(String), New Type() {GetType(String)})
      
      ' First, we'll define the behavior of the "get" property for CustomerName as a method.
      Dim custNameGetPropMthdBldr As MethodBuilder = myTypeBuilder.DefineMethod("GetCustomerName", _
                             MethodAttributes.Public, GetType(String), _
                             New Type() {})
      
      Dim custNameGetIL As ILGenerator = custNameGetPropMthdBldr.GetILGenerator()
      
      custNameGetIL.Emit(OpCodes.Ldarg_0)
      custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr)
      custNameGetIL.Emit(OpCodes.Ret)
      
      ' Now, we'll define the behavior of the "set" property for CustomerName.
      Dim custNameSetPropMthdBldr As MethodBuilder = myTypeBuilder.DefineMethod("SetCustomerName", _
                             MethodAttributes.Public, 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)
      
      
      Return myTypeBuilder.CreateType()
   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'.
' -------------------

[C#] 

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

class PropertyBuilderDemo

{

   public static Type BuildDynamicTypeWithProperties() 
   {
    AppDomain myDomain = Thread.GetDomain();
    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = "MyDynamicAssembly";

    AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(myAsmName,
                            AssemblyBuilderAccess.Run);

    ModuleBuilder myModBuilder = myAsmBuilder.DefineDynamicModule("MyModule");

    TypeBuilder myTypeBuilder = myModBuilder.DefineType("CustomerData", 
                            TypeAttributes.Public);

    FieldBuilder customerNameBldr = myTypeBuilder.DefineField("customerName",
                            typeof(string),
                            FieldAttributes.Private);

    PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName",
                             PropertyAttributes.HasDefault,
                             typeof(string),
                             new Type[] { typeof(string) });

    // First, we'll define the behavior of the "get" property for CustomerName as a method.
    MethodBuilder custNameGetPropMthdBldr = myTypeBuilder.DefineMethod("GetCustomerName",
                            MethodAttributes.Public,    
                            typeof(string),
                            new Type[] { });

    ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();

    custNameGetIL.Emit(OpCodes.Ldarg_0);
    custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr);
    custNameGetIL.Emit(OpCodes.Ret);

    // Now, we'll define the behavior of the "set" property for CustomerName.
    MethodBuilder custNameSetPropMthdBldr = myTypeBuilder.DefineMethod("SetCustomerName",
                            MethodAttributes.Public,    
                            null,
                            new Type[] { typeof(string) });

    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);


    return myTypeBuilder.CreateType();    


   }

   public static void Main() 
   {
    Type custDataType = BuildDynamicTypeWithProperties();
    
    PropertyInfo[] custDataPropInfo = custDataType.GetProperties();
    foreach (PropertyInfo pInfo in custDataPropInfo) {
       Console.WriteLine("Property '{0}' created!", pInfo.ToString());
    }

    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);
    custDataType.InvokeMember("CustomerName", BindingFlags.SetProperty,
                      null, custData, new object[]{ "Joe User" });

    Console.WriteLine("The customerName field of instance custData has been set to '{0}'.",
               custDataType.InvokeMember("CustomerName", BindingFlags.GetProperty,
                              null, custData, new object[]{ }));
   }

}

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


[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

Type* BuildDynamicTypeWithProperties() {
   AppDomain*  myDomain = Thread::GetDomain();
   AssemblyName* myAsmName = new AssemblyName();
   myAsmName->Name = S"MyDynamicAssembly";

   AssemblyBuilder*  myAsmBuilder = myDomain->DefineDynamicAssembly(
      myAsmName,
      AssemblyBuilderAccess::Run);

   ModuleBuilder*  myModBuilder = myAsmBuilder->DefineDynamicModule(S"MyModule");

   TypeBuilder*  myTypeBuilder = myModBuilder->DefineType(
      S"CustomerData",
      TypeAttributes::Public);

   FieldBuilder*  customerNameBldr = myTypeBuilder->DefineField(
      S"customerName",
      __typeof(String),
      FieldAttributes::Private);

   Type* temp0 [] = {__typeof(String)};
   PropertyBuilder*  custNamePropBldr = myTypeBuilder->DefineProperty(
      S"CustomerName",
      PropertyAttributes::HasDefault,
      __typeof(String),
      temp0);

   // First, we'll define the behavior of the "get" property for CustomerName as a method.
   MethodBuilder*  custNameGetPropMthdBldr = myTypeBuilder->DefineMethod(
      S"GetCustomerName",
      MethodAttributes::Public,
      __typeof(String),
      new Type*[0]);

   ILGenerator*  custNameGetIL = custNameGetPropMthdBldr->GetILGenerator();

   custNameGetIL->Emit(OpCodes::Ldarg_0);
   custNameGetIL->Emit(OpCodes::Ldfld, customerNameBldr);
   custNameGetIL->Emit(OpCodes::Ret);

   // Now, we'll define the behavior of the "set" property for CustomerName.

   Type* temp2 [] = {__typeof(String)};
   MethodBuilder*  custNameSetPropMthdBldr = myTypeBuilder->DefineMethod(
      S"SetCustomerName",
      MethodAttributes::Public,
      0,
      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);

   return myTypeBuilder->CreateType();
}

int main() {
   Type*  custDataType = BuildDynamicTypeWithProperties();

   PropertyInfo*  custDataPropInfo[] = custDataType->GetProperties();
   System::Collections::IEnumerator* myEnum = custDataPropInfo->GetEnumerator();
   while (myEnum->MoveNext()) {
      PropertyInfo* pInfo = __try_cast<PropertyInfo*>(myEnum->Current);
      Console::WriteLine(S"Property '{0}' created!", pInfo);
   }

   Console::WriteLine(S"---");
   // 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);

   Object* temp3 [] = {S"Joe User"};
   custDataType->InvokeMember(S"CustomerName", BindingFlags::SetProperty,
      0, custData, temp3);

   Console::WriteLine(S"The customerName field of instance custData has been set to '{0}'.",
      custDataType->InvokeMember(S"CustomerName", BindingFlags::GetProperty,
      0, custData, new 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'.
// -------------------

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Reflection.Emit

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

PropertyBuilder Members | System.Reflection.Emit Namespace

Show: