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

這個屬性 (Property) 的屬性 (Attribute)。

returnType
Type

屬性的傳回類型。

parameterTypes
Type[]

屬性的參數類型。

傳回

定義的屬性。

例外狀況

name 的長度為零。

namenull

-或-

parameterTypes 陣列的所有項目都是 null

先前使用 CreateType() 建立的類型。

範例

下列程式代碼範例示範如何定義動態屬性並取得 PropertyBuilder 規格的 。 請注意, PropertyBuilder 也必須有對應的 MethodBuilder,這會存放 屬性的 IL 邏輯。

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

這個屬性 (Property) 的屬性 (Attribute)。

callingConvention
CallingConventions

屬性存取子的呼叫慣例。

returnType
Type

屬性的傳回類型。

parameterTypes
Type[]

屬性的參數類型。

傳回

定義的屬性。

例外狀況

name 的長度為零。

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

這個屬性 (Property) 的屬性 (Attribute)。

returnType
Type

屬性的傳回類型。

returnTypeRequiredCustomModifiers
Type[]

代表該屬性傳回類型之必要自訂修飾詞的類型陣列,例如 IsConst。 如果傳回類型沒有必要的自訂修飾詞,則指定 null

returnTypeOptionalCustomModifiers
Type[]

代表該屬性傳回型別之選擇性自訂修飾詞的類型陣列,例如 IsConst。 如果傳回類型沒有選擇性的自訂修飾詞,則指定 null

parameterTypes
Type[]

屬性的參數類型。

parameterTypeRequiredCustomModifiers
Type[][]

類型陣列的陣列。 每個類型陣列都代表其對應參數必要的自訂修飾詞,例如 IsConst。 如果特定的參數沒有必要的自訂修飾詞,則指定 null,而非類型陣列。 如果這些參數都沒有必要的自訂修飾詞,則指定 null,而非陣列的陣列。

parameterTypeOptionalCustomModifiers
Type[][]

類型陣列的陣列。 每個類型陣列都代表其對應參數的選擇性自訂修飾詞,例如 IsConst。 如果特定的參數沒有選擇性的自訂修飾詞,則指定 null,而非類型陣列。 如果這些參數都沒有選擇性的自訂修飾詞,則指定 null,而非陣列的陣列。

傳回

定義的屬性。

例外狀況

name 的長度為零。

namenull

-或-

parameterTypes 陣列的所有項目都是 null

先前使用 CreateType() 建立的類型。

備註

這個多載是提供給Managed編譯程式的設計工具。

適用於

.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

這個屬性 (Property) 的屬性 (Attribute)。

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 的長度為零。

namenull

-或-

parameterTypes 陣列的所有項目都是 null

先前使用 CreateType() 建立的類型。

備註

這個多載是提供給Managed編譯程式的設計工具。

此方法多載是在 .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