次の方法で共有


TypeBuilder.DefineProperty メソッド

名前とプロパティ シグネチャを指定して、新しいプロパティをクラスに追加します。

Public Function DefineProperty( _
   ByVal name As String, _   ByVal attributes As PropertyAttributes, _   ByVal returnType As Type, _   ByVal parameterTypes() As Type _) As PropertyBuilder
[C#]
public PropertyBuilder DefineProperty(stringname,PropertyAttributesattributes,TypereturnType,Type[] parameterTypes);
[C++]
public: PropertyBuilder* DefineProperty(String* name,PropertyAttributesattributes,Type* returnType,Type* parameterTypes[]);
[JScript]
public function DefineProperty(
   name : String,attributes : PropertyAttributes,returnType : Type,parameterTypes : Type[]) : PropertyBuilder;

パラメータ

  • name
    プロパティの名前。name には埋め込み null を含めることができません。
  • attributes
    プロパティの属性。
  • returnType
    プロパティの戻り値の型。
  • parameterTypes
    プロパティのパラメータの型。

戻り値

定義されたプロパティ。

例外

例外の種類 条件
ArgumentException name の長さが 0 です。
ArgumentNullException name が null 参照 (Visual Basic では Nothing)

または

parameterTypes 配列のいずれかの要素が null 参照 (Nothing) です。

InvalidOperationException この型は、 CreateType を使用して既に作成されています。

使用例

[Visual Basic, C#, C++] 次のコード例は、仕様に準拠するために、動的なプロパティを定義し、 PropertyBuilder を取得する方法を示しています。 PropertyBuilder には、対応する MethodBuilder (プロパティの IL ロジックを格納) も必要です。

 
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] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間