TypeBuilder.DefineProperty Méthode

Définition

Ajoute une nouvelle propriété au type.

Surcharges

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

Ajoute une nouvelle propriété au type avec le nom et la signature de propriété donnés.

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

Ajoute une nouvelle propriété au type avec le nom, les attributs, la convention d’appel et la signature de propriété spécifiés.

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

Ajoute une nouvelle propriété au type, avec le nom, la signature de propriété et les modificateurs personnalisés donnés.

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

Ajoute une nouvelle propriété au type avec le nom, la convention d’appel, la signature de propriété et les modificateurs personnalisés spécifiés.

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

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Ajoute une nouvelle propriété au type avec le nom et la signature de propriété donnés.

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

Paramètres

name
String

Nom de la propriété. name ne peut pas contenir des valeurs Null imbriquées.

attributes
PropertyAttributes

Attributs de la propriété.

returnType
Type

Type de retour de la propriété.

parameterTypes
Type[]

Types des paramètres de la propriété.

Retours

Propriété définie.

Exceptions

La longueur de name est égale à zéro.

name a la valeur null.

- ou -

Un des éléments du tableau parameterTypes est null.

Le type a déjà été créé en utilisant CreateType().

Exemples

L’exemple de code suivant montre comment définir une propriété dynamique et obtenir un PropertyBuilder pour la spécification. Notez qu’un PropertyBuilder doit également avoir un , MethodBuilderqui hébergera la logique IL pour la propriété.

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

S’applique à

.NET 9 et autres versions
Produit Versions
.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[])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Ajoute une nouvelle propriété au type avec le nom, les attributs, la convention d’appel et la signature de propriété spécifiés.

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

Paramètres

name
String

Nom de la propriété. name ne peut pas contenir des valeurs Null imbriquées.

attributes
PropertyAttributes

Attributs de la propriété.

callingConvention
CallingConventions

Convention d’appel des accesseurs de propriété.

returnType
Type

Type de retour de la propriété.

parameterTypes
Type[]

Types des paramètres de la propriété.

Retours

Propriété définie.

Exceptions

La longueur de name est égale à zéro.

name a la valeur null.

- ou -

Un des éléments du tableau parameterTypes est null.

Le type a déjà été créé en utilisant CreateType().

S’applique à

.NET 9 et autres versions
Produit Versions
.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[][])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Ajoute une nouvelle propriété au type, avec le nom, la signature de propriété et les modificateurs personnalisés donnés.

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

Paramètres

name
String

Nom de la propriété. name ne peut pas contenir des valeurs Null imbriquées.

attributes
PropertyAttributes

Attributs de la propriété.

returnType
Type

Type de retour de la propriété.

returnTypeRequiredCustomModifiers
Type[]

Tableau de types représentant les modificateurs personnalisés obligatoires, comme IsConst, pour le type de retour de la propriété. Si le type de retour ne possède pas de modificateur personnalisé requis, spécifiez null.

returnTypeOptionalCustomModifiers
Type[]

Tableau de types représentant les modificateurs personnalisés facultatifs, comme IsConst, pour le type de retour de la propriété. Si le type de retour ne possède pas de modificateur personnalisé facultatif, spécifiez null.

parameterTypes
Type[]

Types des paramètres de la propriété.

parameterTypeRequiredCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés obligatoires pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés obligatoires, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé requis, spécifiez null plutôt qu’un tableau de tableaux.

parameterTypeOptionalCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés facultatifs pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés facultatifs, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé facultatif, spécifiez null plutôt qu’un tableau de tableaux.

Retours

Propriété définie.

Exceptions

La longueur de name est égale à zéro.

name a la valeur null.

- ou -

Un des éléments du tableau parameterTypes a la valeur null.

Le type a déjà été créé en utilisant CreateType().

Remarques

Cette surcharge est fournie pour les concepteurs de compilateurs managés.

Note

Pour plus d’informations sur les modificateurs personnalisés, consultez Normes ecMA C# et Common Language Infrastructure et Standard ECMA-335 - Common Language Infrastructure (CLI).

S’applique à

.NET 9 et autres versions
Produit Versions
.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[][])

Source:
TypeBuilder.cs
Source:
TypeBuilder.cs
Source:
TypeBuilder.cs

Ajoute une nouvelle propriété au type avec le nom, la convention d’appel, la signature de propriété et les modificateurs personnalisés spécifiés.

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

Paramètres

name
String

Nom de la propriété. name ne peut pas contenir des valeurs Null imbriquées.

attributes
PropertyAttributes

Attributs de la propriété.

callingConvention
CallingConventions

Convention d’appel des accesseurs de propriété.

returnType
Type

Type de retour de la propriété.

returnTypeRequiredCustomModifiers
Type[]

Tableau de types représentant les modificateurs personnalisés obligatoires, comme IsConst, pour le type de retour de la propriété. Si le type de retour ne possède pas de modificateur personnalisé requis, spécifiez null.

returnTypeOptionalCustomModifiers
Type[]

Tableau de types représentant les modificateurs personnalisés facultatifs, comme IsConst, pour le type de retour de la propriété. Si le type de retour ne possède pas de modificateur personnalisé facultatif, spécifiez null.

parameterTypes
Type[]

Types des paramètres de la propriété.

parameterTypeRequiredCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés obligatoires pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés obligatoires, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé requis, spécifiez null plutôt qu’un tableau de tableaux.

parameterTypeOptionalCustomModifiers
Type[][]

Tableau de tableaux de types. Chaque tableau de types représente les modificateurs personnalisés facultatifs pour le paramètre correspondant, comme IsConst. Si un paramètre particulier ne possède pas de modificateurs personnalisés facultatifs, spécifiez null plutôt qu’un tableau de types. Si aucun paramètre ne possède de modificateur personnalisé facultatif, spécifiez null plutôt qu’un tableau de tableaux.

Retours

Propriété définie.

Exceptions

La longueur de name est égale à zéro.

name a la valeur null.

- ou -

Un des éléments du tableau parameterTypes est null.

Le type a déjà été créé en utilisant CreateType().

Remarques

Cette surcharge est fournie pour les concepteurs de compilateurs managés.

Note

Pour plus d’informations sur les modificateurs personnalisés, consultez Normes ecMA C# et Common Language Infrastructure et Standard ECMA-335 - Common Language Infrastructure (CLI).

Cette surcharge de méthode est introduite dans .NET Framework 3.5 ou version ultérieure.

S’applique à

.NET 9 et autres versions
Produit Versions
.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