PropertyBuilder クラス

定義

型のプロパティを定義します。

public ref class PropertyBuilder sealed : System::Reflection::PropertyInfo, System::Runtime::InteropServices::_PropertyBuilder
public ref class PropertyBuilder sealed : System::Reflection::PropertyInfo
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class PropertyBuilder : System.Reflection.PropertyInfo, System.Runtime.InteropServices._PropertyBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class PropertyBuilder : System.Reflection.PropertyInfo, System.Runtime.InteropServices._PropertyBuilder
public sealed class PropertyBuilder : System.Reflection.PropertyInfo
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
type PropertyBuilder = class
    inherit PropertyInfo
    interface _PropertyBuilder
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type PropertyBuilder = class
    inherit PropertyInfo
    interface _PropertyBuilder
type PropertyBuilder = class
    inherit PropertyInfo
Public NotInheritable Class PropertyBuilder
Inherits PropertyInfo
Implements _PropertyBuilder
Public NotInheritable Class PropertyBuilder
Inherits PropertyInfo
継承
PropertyBuilder
属性
実装

次のコード サンプルでは、TypeBuilder.DefineProperty を使用して取得したPropertyBuilderを使用して動的な型にプロパティを実装し、プロパティ フレームワークを作成し、関連付けられたMethodBuilderを使用してプロパティ内に IL ロジックを実装する方法を示します。

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

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

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

        // 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 an array with no elements: new Type[] {})
        PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName",
                                                         PropertyAttributes.HasDefault,
                                                         typeof(string),
                                                         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 CustomerName.
        MethodBuilder custNameGetPropMthdBldr =
            myTypeBuilder.DefineMethod("get_CustomerName",
                                       getSetAttr,
                                       typeof(string),
                                       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.
        MethodBuilder custNameSetPropMthdBldr =
            myTypeBuilder.DefineMethod("set_CustomerName",
                                       getSetAttr,
                                       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);

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

   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' created!
// ---
// The customerName field of instance custData has been set to 'Joe User'.
// -------------------
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
End Class


' --- O U T P U T ---
' The output should be as follows:
' -------------------
' Property 'System.String CustomerName' created!
' ---
' The customerName field of instance custData has been set to 'Joe User'.
' -------------------

注釈

PropertyBuilderは常にTypeBuilderに関連付けられます。 TypeBuilderDefineProperty メソッドはクライアントに新しい PropertyBuilder を返します。

プロパティ

名前 説明
Attributes

このプロパティの属性を取得します。

CanRead

プロパティを読み取ることができるかどうかを示す値を取得します。

CanWrite

プロパティを書き込むことができるかどうかを示す値を取得します。

CustomAttributes

このメンバーのカスタム属性を含むコレクションを取得します。

(継承元 MemberInfo)
DeclaringType

このメンバーを宣言するクラスを取得します。

GetMethod

このプロパティの get アクセサーを取得します。

(継承元 PropertyInfo)
IsSpecialName

プロパティが特別な名前かどうかを示す値を取得します。

(継承元 PropertyInfo)
MemberType

このメンバーがプロパティであることを示す MemberTypes 値を取得します。

(継承元 PropertyInfo)
MetadataToken

メタデータ要素を識別する値を取得します。

(継承元 MemberInfo)
Module

現在のプロパティを宣言する型が定義されているモジュールを取得します。

Name

このメンバーの名前を取得します。

PropertyToken

このプロパティのトークンを取得します。

PropertyType

このプロパティのフィールドの型を取得します。

ReflectedType

MemberInfoのこのインスタンスを取得するために使用されたクラス オブジェクトを取得します。

SetMethod

このプロパティの set アクセサーを取得します。

(継承元 PropertyInfo)

メソッド

名前 説明
AddOtherMethod(MethodBuilder)

このプロパティに関連付けられている他のメソッドのいずれかを追加します。

Equals(Object)

このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。

(継承元 PropertyInfo)
GetAccessors()

現在のインスタンスによって反映されるプロパティのパブリック get および set アクセサーを要素に反映する配列を返します。

(継承元 PropertyInfo)
GetAccessors(Boolean)

このプロパティのパブリックアクセサーとパブリックでない get アクセサーと set アクセサーの配列を返します。

GetConstantValue()

コンパイラによってプロパティに関連付けられているリテラル値を返します。

(継承元 PropertyInfo)
GetCustomAttributes(Boolean)

このプロパティのすべてのカスタム属性の配列を返します。

GetCustomAttributes(Type, Boolean)

Typeによって識別されるカスタム属性の配列を返します。

GetCustomAttributesData()

ターゲット メンバーに適用 CustomAttributeData 属性に関するデータを表すオブジェクトの一覧を返します。

(継承元 MemberInfo)
GetGetMethod()

このプロパティのパブリック get アクセサーを返します。

(継承元 PropertyInfo)
GetGetMethod(Boolean)

このプロパティのパブリックおよびパブリック以外の get アクセサーを返します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 PropertyInfo)
GetIndexParameters()

プロパティのすべてのインデックス パラメーターの配列を返します。

GetOptionalCustomModifiers()

プロパティのオプションのカスタム修飾子を表す型の配列を返します。

(継承元 PropertyInfo)
GetRawConstantValue()

コンパイラによってプロパティに関連付けられているリテラル値を返します。

(継承元 PropertyInfo)
GetRequiredCustomModifiers()

プロパティの必要なカスタム修飾子を表す型の配列を返します。

(継承元 PropertyInfo)
GetSetMethod()

このプロパティのパブリック set アクセサーを返します。

(継承元 PropertyInfo)
GetSetMethod(Boolean)

このプロパティの set アクセサーを返します。

GetType()

プロパティの属性を検出し、プロパティ メタデータへのアクセスを提供します。

(継承元 PropertyInfo)
GetValue(Object, BindingFlags, Binder, Object[], CultureInfo)

指定したバインディング、インデックス、および CultureInfoを持つプロパティの値を取得します。

GetValue(Object, Object[])

プロパティの getter メソッドを呼び出して、インデックス付きプロパティの値を取得します。

GetValue(Object)

指定したオブジェクトのプロパティ値を返します。

(継承元 PropertyInfo)
HasSameMetadataDefinitionAs(MemberInfo)

型のプロパティを定義します。

(継承元 MemberInfo)
IsDefined(Type, Boolean)

attributeTypeの 1 つ以上のインスタンスがこのプロパティで定義されているかどうかを示します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
SetConstant(Object)

このプロパティの既定値を設定します。

SetCustomAttribute(ConstructorInfo, Byte[])

指定したカスタム属性 BLOB を使用してカスタム属性を設定します。

SetCustomAttribute(CustomAttributeBuilder)

カスタム属性ビルダーを使用してカスタム属性を設定します。

SetGetMethod(MethodBuilder)

プロパティ値を取得するメソッドを設定します。

SetSetMethod(MethodBuilder)

プロパティ値を設定するメソッドを設定します。

SetValue(Object, Object, BindingFlags, Binder, Object[], CultureInfo)

指定したオブジェクトのプロパティ値を指定した値に設定します。

SetValue(Object, Object, Object[])

インデックス プロパティのオプションのインデックス値を使用して、プロパティの値を設定します。

SetValue(Object, Object)

指定したオブジェクトのプロパティ値を設定します。

(継承元 PropertyInfo)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
_MemberInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 MemberInfo)
_MemberInfo.GetType()

Type クラスを表すMemberInfo オブジェクトを取得します。

(継承元 MemberInfo)
_MemberInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

(継承元 MemberInfo)
_MemberInfo.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 MemberInfo)
_MemberInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 MemberInfo)
_PropertyBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

_PropertyBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

_PropertyBuilder.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

_PropertyBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

_PropertyInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 PropertyInfo)
_PropertyInfo.GetType()

Type型を表すPropertyInfo オブジェクトを取得します。

(継承元 PropertyInfo)
_PropertyInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この型情報を使用して、インターフェイスの型情報を取得できます。

(継承元 PropertyInfo)
_PropertyInfo.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 PropertyInfo)
_PropertyInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されるプロパティとメソッドへのアクセスを提供します。

(継承元 PropertyInfo)

拡張メソッド

名前 説明
GetCustomAttribute(MemberInfo, Type, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性を取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttribute(MemberInfo, Type)

指定したメンバーに適用される、指定した型のカスタム属性を取得します。

GetCustomAttribute<T>(MemberInfo, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性を取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttribute<T>(MemberInfo)

指定したメンバーに適用される、指定した型のカスタム属性を取得します。

GetCustomAttributes(MemberInfo, Boolean)

指定したメンバーに適用されるカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes(MemberInfo, Type, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes(MemberInfo, Type)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得します。

GetCustomAttributes(MemberInfo)

指定したメンバーに適用されるカスタム属性のコレクションを取得します。

GetCustomAttributes<T>(MemberInfo, Boolean)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得し、必要に応じてそのメンバーの先祖を検査します。

GetCustomAttributes<T>(MemberInfo)

指定したメンバーに適用される、指定した型のカスタム属性のコレクションを取得します。

IsDefined(MemberInfo, Type, Boolean)

指定した型のカスタム属性が指定したメンバーに適用され、必要に応じてその先祖に適用されるかどうかを示します。

IsDefined(MemberInfo, Type)

指定した型のカスタム属性が、指定したメンバーに適用されるかどうかを示します。

適用対象