英語で読む

次の方法で共有


TypeBuilder.DefineProperty メソッド

定義

型に新しいプロパティを追加します。

オーバーロード

DefineProperty(String, PropertyAttributes, Type, Type[])

指定された名前とプロパティのシグネチャにより、新しいプロパティを型に追加します。

DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[])

新しいプロパティを、指定された名前、属性、呼び出し規則、およびプロパティの署名を持つ型に追加します。

DefineProperty(String, PropertyAttributes, Type, Type[], Type[], Type[], Type[][], Type[][])

指定された名前、プロパティのシグネチャ、およびカスタム修飾子により、新しいプロパティを型に追加します。

DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

指定された名前、呼び出し規則、プロパティの署名、およびカスタム修飾子を使用して、新しいプロパティを型に追加します。

DefineProperty(String, PropertyAttributes, Type, Type[])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

指定された名前とプロパティのシグネチャにより、新しいプロパティを型に追加します。

C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[]? parameterTypes);
C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[] parameterTypes);

パラメーター

name
String

プロパティの名前。 name に埋め込み null 値を含めることはできません。

attributes
PropertyAttributes

プロパティの属性。

returnType
Type

プロパティの戻り値の型。

parameterTypes
Type[]

プロパティのパラメーターの型。

戻り値

定義されたプロパティ。

例外

name の長さが 0 です。

namenullです。

- または -

parameterTypes 配列の要素のいずれかが null です。

型は CreateType() を使用して既に作成されました。

次のコード サンプルは、動的プロパティを定義し、仕様の を PropertyBuilder 取得する方法を示しています。 PropertyBuilderには、 プロパティの IL ロジックを格納する対応する MethodBuilderも必要であることに注意してください。

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

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

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

新しいプロパティを、指定された名前、属性、呼び出し規則、およびプロパティの署名を持つ型に追加します。

C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[]? parameterTypes);
C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);

パラメーター

name
String

プロパティの名前。 name に埋め込み null 値を含めることはできません。

attributes
PropertyAttributes

プロパティの属性。

callingConvention
CallingConventions

プロパティ アクセサーの呼び出し規則。

returnType
Type

プロパティの戻り値の型。

parameterTypes
Type[]

プロパティのパラメーターの型。

戻り値

定義されたプロパティ。

例外

name の長さが 0 です。

namenullです。

- または -

parameterTypes 配列の要素のいずれかが null です。

型は CreateType() を使用して既に作成されました。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DefineProperty(String, PropertyAttributes, Type, Type[], Type[], Type[], Type[][], Type[][])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

指定された名前、プロパティのシグネチャ、およびカスタム修飾子により、新しいプロパティを型に追加します。

C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers);

パラメーター

name
String

プロパティの名前。 name に埋め込み null 値を含めることはできません。

attributes
PropertyAttributes

プロパティの属性。

returnType
Type

プロパティの戻り値の型。

returnTypeRequiredCustomModifiers
Type[]

プロパティの戻り値の型の IsConst など、必須のカスタム修飾子を表す型の配列。 戻り値の型が必須のカスタム修飾子を持たない場合は、null を指定します。

returnTypeOptionalCustomModifiers
Type[]

プロパティの戻り値の型の IsConst のような、省略可能なカスタム修飾子を表す型の配列。 戻り値の型に省略可能のカスタム修飾子がない場合は、null を指定します。

parameterTypes
Type[]

プロパティのパラメーターの型。

parameterTypeRequiredCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、IsConst のような、対応するパラメーターの必須のカスタム修飾子を表します。 特定のパラメーターに必須のカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも必須のカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

parameterTypeOptionalCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、 IsConstのような、対応するパラメーターの省略可能なカスタム修飾子を表します。 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも省略可能なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

戻り値

定義されたプロパティ。

例外

name の長さが 0 です。

namenull です

- または -

parameterTypes 配列の要素のいずれかが null です。

型は CreateType() を使用して既に作成されました。

注釈

このオーバーロードは、マネージド コンパイラのデザイナー向けに提供されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs
ソース:
TypeBuilder.cs

指定された名前、呼び出し規則、プロパティの署名、およびカスタム修飾子を使用して、新しいプロパティを型に追加します。

C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
C#
public System.Reflection.Emit.PropertyBuilder DefineProperty (string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers);

パラメーター

name
String

プロパティの名前。 name に埋め込み null 値を含めることはできません。

attributes
PropertyAttributes

プロパティの属性。

callingConvention
CallingConventions

プロパティ アクセサーの呼び出し規則。

returnType
Type

プロパティの戻り値の型。

returnTypeRequiredCustomModifiers
Type[]

プロパティの戻り値の型の IsConst など、必須のカスタム修飾子を表す型の配列。 戻り値の型が必須のカスタム修飾子を持たない場合は、null を指定します。

returnTypeOptionalCustomModifiers
Type[]

プロパティの戻り値の型の IsConst のような、省略可能なカスタム修飾子を表す型の配列。 戻り値の型に省略可能のカスタム修飾子がない場合は、null を指定します。

parameterTypes
Type[]

プロパティのパラメーターの型。

parameterTypeRequiredCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、IsConst のような、対応するパラメーターの必須のカスタム修飾子を表します。 特定のパラメーターに必須のカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも必須のカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

parameterTypeOptionalCustomModifiers
Type[][]

型の配列の配列。 型の各配列は、 IsConstのような、対応するパラメーターの省略可能なカスタム修飾子を表します。 特定のパラメーターに省略可能なカスタム修飾子がない場合は、型の配列の代わりに null を指定します。 どのパラメーターにも省略可能なカスタム修飾子がない場合は、配列の配列の代わりに null を指定します。

戻り値

定義されたプロパティ。

例外

name の長さが 0 です。

namenullです。

- または -

parameterTypes 配列の要素のいずれかが null です。

型は CreateType() を使用して既に作成されました。

注釈

このオーバーロードは、マネージド コンパイラのデザイナー向けに提供されます。

このメソッド オーバーロードは、.NET Framework 3.5 以降で導入されています。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1