TypeBuilder.DefineProperty TypeBuilder.DefineProperty TypeBuilder.DefineProperty TypeBuilder.DefineProperty Method

定义

向此类型添加一个新的属性。Adds a new property to the type.

重载

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

将新属性添加到具有给定名称和属性签名的类型中。Adds a new property to the type, with the given name and property signature.

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

将新属性添加到具有给定名称、属性、调用约定和属性签名的类型。Adds a new property to the type, with the given name, attributes, calling convention, and property signature.

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

将新属性添加到具有给定名称、属性签名和自定义修饰符的类型。Adds a new property to the type, with the given name, property signature, and custom modifiers.

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

将新属性添加到具有给定名称、调用约定、属性签名和自定义修饰符的类型。Adds a new property to the type, with the given name, calling convention, property signature, and custom modifiers.

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

将新属性添加到具有给定名称和属性签名的类型中。Adds a new property to the type, with the given name and property signature.

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

属性的名称。The name of the property. name 不能包含嵌入的 null。name cannot contain embedded nulls.

attributes
PropertyAttributes PropertyAttributes PropertyAttributes PropertyAttributes

属性的特性。The attributes of the property.

returnType
Type Type Type Type

属性的返回类型。The return type of the property.

parameterTypes
Type[]

属性的参数类型。The types of the parameters of the property.

返回

定义的属性。The defined property.

异常

name 的长度为零。The length of name is zero.

namenullname is null.

-or- parameterTypes 数组的任何元素都是 nullAny of the elements of the parameterTypes array is null.

该类型是以前使用 CreateType() 创建的。The type was previously created using CreateType().

示例

下面的代码示例演示如何定义动态属性并获取PropertyBuilder规范的。The following code sample demonstrates how to define a dynamic property and obtain a PropertyBuilder for specification. 请注意,还MethodBuilder必须具有相应的,它将承载属性的IL逻辑。PropertyBuilderNote that a PropertyBuilder must also have a corresponding MethodBuilder, which will house the IL logic for the property.

using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
Type^ BuildDynamicTypeWithProperties()
{
   AppDomain^ myDomain = Thread::GetDomain();
   AssemblyName^ myAsmName = gcnew 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 );

   // Define a private field to hold the property value.
   FieldBuilder^ customerNameBldr = myTypeBuilder->DefineField( "customerName", String::typeid, FieldAttributes::Private );
   
   // The last argument of DefineProperty is an empty array of Type
   // objects, because the property has no parameters. (Alternatively,
   // you can specify a null value.)
   PropertyBuilder^ custNamePropBldr = 
       myTypeBuilder->DefineProperty( "CustomerName", PropertyAttributes::HasDefault, String::typeid, gcnew array<Type^>(0) );
   
   // 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,
                                    String::typeid, 
                                    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.
   array<Type^>^temp2 = {String::typeid};
   MethodBuilder^ custNameSetPropMthdBldr = 
       myTypeBuilder->DefineMethod( "set_CustomerName", 
                                    getSetAttr,
                                    nullptr, 
                                    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 );

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

int main()
{
   Type^ custDataType = BuildDynamicTypeWithProperties();
   array<PropertyInfo^>^custDataPropInfo = custDataType->GetProperties();
   System::Collections::IEnumerator^ myEnum = custDataPropInfo->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      PropertyInfo^ pInfo = safe_cast<PropertyInfo^>(myEnum->Current);
      Console::WriteLine( "Property '{0}' created!", 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.
   Object^ custData = Activator::CreateInstance( custDataType );
   array<Object^>^temp3 = {"Joe User"};
   custDataType->InvokeMember( "CustomerName", BindingFlags::SetProperty, nullptr, custData, temp3 );
   Console::WriteLine( "The customerName field of instance custData has been set to '{0}'.", custDataType->InvokeMember( "CustomerName", BindingFlags::GetProperty, nullptr, custData, gcnew array<Object^>(0) ) );
}

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

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 'Main
End Class 'PropertyBuilderDemo


' --- 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[]) DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[]) DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[]) DefineProperty(String, PropertyAttributes, CallingConventions, Type, Type[])

将新属性添加到具有给定名称、属性、调用约定和属性签名的类型。Adds a new property to the type, with the given name, attributes, calling convention, and property signature.

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

属性的名称。The name of the property. name 不能包含嵌入的 null。name cannot contain embedded nulls.

attributes
PropertyAttributes PropertyAttributes PropertyAttributes PropertyAttributes

属性的特性。The attributes of the property.

callingConvention
CallingConventions CallingConventions CallingConventions CallingConventions

属性访问器的调用约定。The calling convention of the property accessors.

returnType
Type Type Type Type

属性的返回类型。The return type of the property.

parameterTypes
Type[]

属性的参数类型。The types of the parameters of the property.

返回

定义的属性。The defined property.

异常

name 的长度为零。The length of name is zero.

namenullname is null.

-or- parameterTypes 数组的任何元素都是 nullAny of the elements of the parameterTypes array is null.

该类型是以前使用 CreateType() 创建的。The type was previously created using CreateType().

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

将新属性添加到具有给定名称、属性签名和自定义修饰符的类型。Adds a new property to the type, with the given name, property signature, and custom modifiers.

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

属性的名称。The name of the property. name 不能包含嵌入的 null。name cannot contain embedded nulls.

attributes
PropertyAttributes PropertyAttributes PropertyAttributes PropertyAttributes

属性的特性。The attributes of the property.

returnType
Type Type Type Type

属性的返回类型。The return type of the property.

returnTypeRequiredCustomModifiers
Type[]

一个类型数组,它表示属性的返回类型所必需的自定义修饰符,如 IsConstAn array of types representing the required custom modifiers, such as IsConst, for the return type of the property. 如果返回类型没有所需的自定义修饰符,则指定 nullIf the return type has no required custom modifiers, specify null.

returnTypeOptionalCustomModifiers
Type[]

一个类型数组,它表示属性的返回类型的可选自定义修饰符,如 IsConstAn array of types representing the optional custom modifiers, such as IsConst, for the return type of the property. 如果返回类型没有可选的自定义修饰符,则指定 nullIf the return type has no optional custom modifiers, specify null.

parameterTypes
Type[]

属性的参数类型。The types of the parameters of the property.

parameterTypeRequiredCustomModifiers
Type[][]

由类型数组组成的数组。An array of arrays of types. 每个类型数组均表示相应参数所必需的自定义修饰符,如 IsConstEach array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. 如果某个特定参数没有所需的自定义修饰符,则指定 null,而不要指定类型数组。If a particular parameter has no required custom modifiers, specify null instead of an array of types. 如果所有参数都没有所需的自定义修饰符,则指定 null,而不要指定由数组组成的数组。If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

parameterTypeOptionalCustomModifiers
Type[][]

由类型数组组成的数组。An array of arrays of types. 每个类型数组均表示相应参数的可选自定义修饰符,如 IsConstEach array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. 如果某个特定参数没有可选的自定义修饰符,则指定 null,而不要指定类型数组。If a particular parameter has no optional custom modifiers, specify null instead of an array of types. 如果所有参数都没有可选的自定义修饰符,则指定 null,而不要指定由数组组成的数组。If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

返回

定义的属性。The defined property.

异常

name 的长度为零。The length of name is zero.

namenullname is null

-or- parameterTypes 数组的任何元素都是 nullAny of the elements of the parameterTypes array is null

该类型是以前使用 CreateType() 创建的。The type was previously created using CreateType().

注解

此重载是为托管编译器的设计器提供的。This overload is provided for designers of managed compilers.

备注

有关自定义修饰符的详细信息,请参阅 ECMA Partition II 元数据文档。For more information on custom modifiers, see the ECMA Partition II Metadata documentation. 可联机获取该文档;请参阅 MSDN 上的 ECMA C# 和公共语言基础结构标准和 Ecma International 网站上的标准 ECMA-335 - 公共语言基础结构 (CLI)The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

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

将新属性添加到具有给定名称、调用约定、属性签名和自定义修饰符的类型。Adds a new property to the type, with the given name, calling convention, property signature, and custom modifiers.

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

属性的名称。The name of the property. name 不能包含嵌入的 null。name cannot contain embedded nulls.

attributes
PropertyAttributes PropertyAttributes PropertyAttributes PropertyAttributes

属性的特性。The attributes of the property.

callingConvention
CallingConventions CallingConventions CallingConventions CallingConventions

属性访问器的调用约定。The calling convention of the property accessors.

returnType
Type Type Type Type

属性的返回类型。The return type of the property.

returnTypeRequiredCustomModifiers
Type[]

一个类型数组,它表示属性的返回类型所必需的自定义修饰符,如 IsConstAn array of types representing the required custom modifiers, such as IsConst, for the return type of the property. 如果返回类型没有所需的自定义修饰符,则指定 nullIf the return type has no required custom modifiers, specify null.

returnTypeOptionalCustomModifiers
Type[]

一个类型数组,它表示属性的返回类型的可选自定义修饰符,如 IsConstAn array of types representing the optional custom modifiers, such as IsConst, for the return type of the property. 如果返回类型没有可选的自定义修饰符,则指定 nullIf the return type has no optional custom modifiers, specify null.

parameterTypes
Type[]

属性的参数类型。The types of the parameters of the property.

parameterTypeRequiredCustomModifiers
Type[][]

由类型数组组成的数组。An array of arrays of types. 每个类型数组均表示相应参数所必需的自定义修饰符,如 IsConstEach array of types represents the required custom modifiers for the corresponding parameter, such as IsConst. 如果某个特定参数没有所需的自定义修饰符,则指定 null,而不要指定类型数组。If a particular parameter has no required custom modifiers, specify null instead of an array of types. 如果所有参数都没有所需的自定义修饰符,则指定 null,而不要指定由数组组成的数组。If none of the parameters have required custom modifiers, specify null instead of an array of arrays.

parameterTypeOptionalCustomModifiers
Type[][]

由类型数组组成的数组。An array of arrays of types. 每个类型数组均表示相应参数的可选自定义修饰符,如 IsConstEach array of types represents the optional custom modifiers for the corresponding parameter, such as IsConst. 如果某个特定参数没有可选的自定义修饰符,则指定 null,而不要指定类型数组。If a particular parameter has no optional custom modifiers, specify null instead of an array of types. 如果所有参数都没有可选的自定义修饰符,则指定 null,而不要指定由数组组成的数组。If none of the parameters have optional custom modifiers, specify null instead of an array of arrays.

返回

定义的属性。The defined property.

异常

name 的长度为零。The length of name is zero.

namenullname is null.

-or- parameterTypes 数组的任何元素都是 nullAny of the elements of the parameterTypes array is null.

该类型是以前使用 CreateType() 创建的。The type was previously created using CreateType().

注解

此重载是为托管编译器的设计器提供的。This overload is provided for designers of managed compilers.

备注

有关自定义修饰符的详细信息,请参阅 ECMA Partition II 元数据文档。For more information on custom modifiers, see the ECMA Partition II Metadata documentation. 可联机获取该文档;请参阅 MSDN 上的 ECMA C# 和公共语言基础结构标准和 Ecma International 网站上的标准 ECMA-335 - 公共语言基础结构 (CLI)The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the Ecma International Web site.

此方法重载在 .NET Framework 3.5.NET Framework 3.5或更高版本中引入。This method overload is introduced in the .NET Framework 3.5.NET Framework 3.5 or later.

适用于