TypeBuilder.DefineProperty Metoda

Definicja

Dodaje nową właściwość do typu.

Przeciążenia

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

Dodaje nową właściwość do typu z podaną nazwą i podpisem właściwości.

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

Dodaje nową właściwość do typu z daną nazwą, atrybutami, konwencją wywoływania i podpisem właściwości.

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

Dodaje nową właściwość do typu z daną nazwą, podpisem właściwości i modyfikatorami niestandardowymi.

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

Dodaje nową właściwość do typu z daną nazwą, konwencją wywoływania, podpisem właściwości i modyfikatorami niestandardowymi.

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

Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs

Dodaje nową właściwość do typu z podaną nazwą i podpisem właściwości.

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

Parametry

name
String

Nazwa właściwości. name nie może zawierać osadzonych wartości null.

attributes
PropertyAttributes

Atrybuty właściwości.

returnType
Type

Zwracany typ właściwości.

parameterTypes
Type[]

Typy parametrów właściwości.

Zwraca

Zdefiniowana właściwość.

Wyjątki

Długość name to zero.

name to null.

-lub-

Dowolny z elementów tablicy parameterTypes to null.

Typ został wcześniej utworzony przy użyciu polecenia CreateType().

Przykłady

W poniższym przykładzie kodu pokazano, jak zdefiniować właściwość dynamiczną i uzyskać dla PropertyBuilder specyfikacji. Należy pamiętać, że element PropertyBuilder musi również mieć odpowiedni MethodBuilderelement , który będzie zawierać logikę IL dla właściwości.

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

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.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[])

Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs

Dodaje nową właściwość do typu z daną nazwą, atrybutami, konwencją wywoływania i podpisem właściwości.

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

Parametry

name
String

Nazwa właściwości. name nie może zawierać osadzonych wartości null.

attributes
PropertyAttributes

Atrybuty właściwości.

callingConvention
CallingConventions

Konwencja wywoływania akcesoriów właściwości.

returnType
Type

Zwracany typ właściwości.

parameterTypes
Type[]

Typy parametrów właściwości.

Zwraca

Zdefiniowana właściwość.

Wyjątki

Długość name to zero.

name to null.

-lub-

Dowolny z elementów tablicy parameterTypes to null.

Typ został wcześniej utworzony przy użyciu polecenia CreateType().

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.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[][])

Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs

Dodaje nową właściwość do typu z daną nazwą, podpisem właściwości i modyfikatorami niestandardowymi.

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

Parametry

name
String

Nazwa właściwości. name nie może zawierać osadzonych wartości null.

attributes
PropertyAttributes

Atrybuty właściwości.

returnType
Type

Zwracany typ właściwości.

returnTypeRequiredCustomModifiers
Type[]

Tablica typów reprezentujących wymagane modyfikatory niestandardowe, takie jak IsConst, dla zwracanego typu właściwości. Jeśli typ zwracany nie ma wymaganych modyfikatorów niestandardowych, określ nullwartość .

returnTypeOptionalCustomModifiers
Type[]

Tablica typów reprezentujących opcjonalne modyfikatory niestandardowe, takie jak IsConst, dla zwracanego typu właściwości. Jeśli typ zwracany nie ma opcjonalnych modyfikatorów niestandardowych, określ nullwartość .

parameterTypes
Type[]

Typy parametrów właściwości.

parameterTypeRequiredCustomModifiers
Type[][]

Tablica tablic typów. Każda tablica typów reprezentuje wymagane modyfikatory niestandardowe dla odpowiedniego parametru, na przykład IsConst. Jeśli określony parametr nie ma wymaganych modyfikatorów niestandardowych, określ null zamiast tablicy typów. Jeśli żaden z parametrów nie ma wymaganych modyfikatorów niestandardowych, określ null zamiast tablicy tablic.

parameterTypeOptionalCustomModifiers
Type[][]

Tablica tablic typów. Każda tablica typów reprezentuje opcjonalne modyfikatory niestandardowe dla odpowiedniego parametru, na przykład IsConst. Jeśli określony parametr nie ma opcjonalnych modyfikatorów niestandardowych, określ null zamiast tablicy typów. Jeśli żaden z parametrów nie ma opcjonalnych modyfikatorów niestandardowych, określ null zamiast tablicy tablic.

Zwraca

Zdefiniowana właściwość.

Wyjątki

Długość name to zero.

name to null

-lub-

Dowolny z elementów tablicy parameterTypes jest null

Typ został wcześniej utworzony przy użyciu polecenia CreateType().

Uwagi

To przeciążenie jest udostępniane projektantom zarządzanych kompilatorów.

Uwaga

Aby uzyskać więcej informacji na temat modyfikatorów niestandardowych, zobacz ECMA C# i Common Language Infrastructure Standards andStandard ECMA-335 - Common Language Infrastructure (CLI).

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.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[][])

Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs
Źródło:
TypeBuilder.cs

Dodaje nową właściwość do typu z daną nazwą, konwencją wywoływania, podpisem właściwości i modyfikatorami niestandardowymi.

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

Parametry

name
String

Nazwa właściwości. name nie może zawierać osadzonych wartości null.

attributes
PropertyAttributes

Atrybuty właściwości.

callingConvention
CallingConventions

Konwencja wywoływania akcesoriów właściwości.

returnType
Type

Zwracany typ właściwości.

returnTypeRequiredCustomModifiers
Type[]

Tablica typów reprezentujących wymagane modyfikatory niestandardowe, takie jak IsConst, dla zwracanego typu właściwości. Jeśli typ zwracany nie ma wymaganych modyfikatorów niestandardowych, określ nullwartość .

returnTypeOptionalCustomModifiers
Type[]

Tablica typów reprezentujących opcjonalne modyfikatory niestandardowe, takie jak IsConst, dla zwracanego typu właściwości. Jeśli typ zwracany nie ma opcjonalnych modyfikatorów niestandardowych, określ nullwartość .

parameterTypes
Type[]

Typy parametrów właściwości.

parameterTypeRequiredCustomModifiers
Type[][]

Tablica tablic typów. Każda tablica typów reprezentuje wymagane modyfikatory niestandardowe dla odpowiedniego parametru, na przykład IsConst. Jeśli określony parametr nie ma wymaganych modyfikatorów niestandardowych, określ null zamiast tablicy typów. Jeśli żaden z parametrów nie ma wymaganych modyfikatorów niestandardowych, określ null zamiast tablicy tablic.

parameterTypeOptionalCustomModifiers
Type[][]

Tablica tablic typów. Każda tablica typów reprezentuje opcjonalne modyfikatory niestandardowe dla odpowiedniego parametru, na przykład IsConst. Jeśli określony parametr nie ma opcjonalnych modyfikatorów niestandardowych, określ null zamiast tablicy typów. Jeśli żaden z parametrów nie ma opcjonalnych modyfikatorów niestandardowych, określ null zamiast tablicy tablic.

Zwraca

Zdefiniowana właściwość.

Wyjątki

Długość name to zero.

name to null.

-lub-

Dowolny z elementów tablicy parameterTypes to null.

Typ został wcześniej utworzony przy użyciu polecenia CreateType().

Uwagi

To przeciążenie jest udostępniane projektantom zarządzanych kompilatorów.

Uwaga

Aby uzyskać więcej informacji na temat modyfikatorów niestandardowych, zobacz ECMA C# i Common Language Infrastructure Standards andStandard ECMA-335 - Common Language Infrastructure (CLI).

To przeciążenie metody jest wprowadzane w .NET Framework 3.5 lub nowszym.

Dotyczy

.NET 9 i inne wersje
Produkt Wersje
.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