TypeBuilder.DefineProperty 方法

定義

為該類型新增一個屬性。

多載

名稱 Description
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
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

為型別新增一個屬性,包含名稱與屬性簽名。

public:
 System::Reflection::Emit::PropertyBuilder ^ DefineProperty(System::String ^ name, System::Reflection::PropertyAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[]? parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[] parameterTypes);
member this.DefineProperty : string * System.Reflection.PropertyAttributes * Type * Type[] -> System.Reflection.Emit.PropertyBuilder
Public Function DefineProperty (name As String, attributes As PropertyAttributes, returnType As Type, parameterTypes As Type()) As PropertyBuilder

參數

name
String

屬性的名稱。 name 無法包含嵌入的空。

attributes
PropertyAttributes

房產的屬性。

returnType
Type

屬性的回報類型。

parameterTypes
Type[]

屬性參數的類型。

傳回

定義的性質。

例外狀況

name 長度為零。

namenull

-或-

陣列中的 parameterTypes 任一元素皆為 null

該類型先前是使用 CreateType()

範例

以下範例示範如何定義動態屬性並取得 PropertyBuilder a for specification。 注意 a 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'.
' -------------------

適用於

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

來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

為型別新增一個屬性,包含名稱、屬性、呼叫慣例及屬性簽名。

public:
 System::Reflection::Emit::PropertyBuilder ^ DefineProperty(System::String ^ name, System::Reflection::PropertyAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[]? parameterTypes);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.DefineProperty : string * System.Reflection.PropertyAttributes * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.Emit.PropertyBuilder
Public Function DefineProperty (name As String, attributes As PropertyAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As PropertyBuilder

參數

name
String

屬性的名稱。 name 無法包含嵌入的空。

attributes
PropertyAttributes

房產的屬性。

callingConvention
CallingConventions

屬性存取器的呼叫慣例。

returnType
Type

屬性的回報類型。

parameterTypes
Type[]

屬性參數的類型。

傳回

定義的性質。

例外狀況

name 長度為零。

namenull

-或-

陣列中的 parameterTypes 任一元素皆為 null

該類型先前是使用 CreateType()

適用於

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

來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

為型別新增一個屬性,包含名稱、屬性簽名和自訂修飾符。

public:
 System::Reflection::Emit::PropertyBuilder ^ DefineProperty(System::String ^ name, System::Reflection::PropertyAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.PropertyBuilder DefineProperty(string name, System.Reflection.PropertyAttributes attributes, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers);
member this.DefineProperty : string * System.Reflection.PropertyAttributes * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.PropertyBuilder
Public Function DefineProperty (name As String, attributes As PropertyAttributes, returnType As Type, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()()) As PropertyBuilder

參數

name
String

屬性的名稱。 name 無法包含嵌入的空。

attributes
PropertyAttributes

房產的屬性。

returnType
Type

屬性的回報類型。

returnTypeRequiredCustomModifiers
Type[]

一組代表屬性回傳類型所需的自訂修飾符的陣列,例如 IsConst。 若返回類型沒有必要的自訂修飾符,請指定 null

returnTypeOptionalCustomModifiers
Type[]

一組代表可選自訂修飾符的型別陣列,例如 IsConst,代表屬性的回傳型別。 若返回類型沒有可選的自訂修飾符,請指定 null

parameterTypes
Type[]

屬性參數的類型。

parameterTypeRequiredCustomModifiers
Type[][]

一組類型的陣列。 每個類型陣列代表對應參數所需的自訂修飾符,例如 IsConst。 如果某個參數沒有必要的自訂修飾符,則改用 Display null 代替型別陣列。 如果這些參數都不需要自訂修飾符,請指定 null 為 代替陣列的陣列。

parameterTypeOptionalCustomModifiers
Type[][]

一組類型的陣列。 每個類型陣列代表對應參數的可選自訂修飾符,例如 IsConst。 如果某個參數沒有可選的自訂修飾符,則用指定 null 代替型別陣列。 如果沒有任何參數有可選的自訂修飾符,則指定 null 為 ,而不是陣列的陣列。

傳回

定義的性質。

例外狀況

name 長度為零。

namenull

-或-

陣列中的 parameterTypes 任一元素為 null

該類型先前是使用 CreateType()

備註

這種過載是為受管編譯器設計者提供的。

Note

欲了解更多自訂修飾符的資訊,請參閱 ECMA C# 與通用語言基礎設施標準,以及標準 ECMA-335 - 通用語言基礎架構(CLI)。

適用於

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

來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs
來源:
TypeBuilder.cs

在型別中新增一個屬性,包含名稱、呼叫慣例、屬性簽名和自訂修飾符。

public:
 System::Reflection::Emit::PropertyBuilder ^ DefineProperty(System::String ^ name, System::Reflection::PropertyAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers);
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);
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);
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);
member this.DefineProperty : string * System.Reflection.PropertyAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.PropertyBuilder
Public Function DefineProperty (name As String, attributes As PropertyAttributes, callingConvention As CallingConventions, returnType As Type, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()()) As PropertyBuilder

參數

name
String

屬性的名稱。 name 無法包含嵌入的空。

attributes
PropertyAttributes

房產的屬性。

callingConvention
CallingConventions

屬性存取器的呼叫慣例。

returnType
Type

屬性的回報類型。

returnTypeRequiredCustomModifiers
Type[]

一組代表屬性回傳類型所需的自訂修飾符的陣列,例如 IsConst。 若返回類型沒有必要的自訂修飾符,請指定 null

returnTypeOptionalCustomModifiers
Type[]

一組代表可選自訂修飾符的型別陣列,例如 IsConst,代表屬性的回傳型別。 若返回類型沒有可選的自訂修飾符,請指定 null

parameterTypes
Type[]

屬性參數的類型。

parameterTypeRequiredCustomModifiers
Type[][]

一組類型的陣列。 每個類型陣列代表對應參數所需的自訂修飾符,例如 IsConst。 如果某個參數沒有必要的自訂修飾符,則改用 Display null 代替型別陣列。 如果這些參數都不需要自訂修飾符,請指定 null 為 代替陣列的陣列。

parameterTypeOptionalCustomModifiers
Type[][]

一組類型的陣列。 每個類型陣列代表對應參數的可選自訂修飾符,例如 IsConst。 如果某個參數沒有可選的自訂修飾符,則用指定 null 代替型別陣列。 如果沒有任何參數有可選的自訂修飾符,則指定 null 為 ,而不是陣列的陣列。

傳回

定義的性質。

例外狀況

name 長度為零。

namenull

-或-

陣列中的 parameterTypes 任一元素皆為 null

該類型先前是使用 CreateType()

備註

這種過載是為受管編譯器設計者提供的。

Note

欲了解更多自訂修飾符的資訊,請參閱 ECMA C# 與通用語言基礎設施標準,以及標準 ECMA-335 - 通用語言基礎架構(CLI)。

此方法超載是在 .NET Framework 3.5 或更新版本中引入的。

適用於