TypeBuilder.DefineConstructor Metoda

Definicja

Dodaje nowy konstruktor do typu dynamicznego.

Przeciążenia

DefineConstructor(MethodAttributes, CallingConventions, Type[])

Dodaje nowy konstruktor do typu z podanymi atrybutami i podpisem.

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

Dodaje nowy konstruktor do typu z podanymi atrybutami, podpisem i modyfikatorami niestandardowymi.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

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

Dodaje nowy konstruktor do typu z podanymi atrybutami i podpisem.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type()) As ConstructorBuilder

Parametry

attributes
MethodAttributes

Atrybuty konstruktora.

callingConvention
CallingConventions

Konwencja wywoływania konstruktora.

parameterTypes
Type[]

Typy parametrów konstruktora.

Zwraca

Zdefiniowany konstruktor.

Atrybuty

Wyjątki

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

Przykłady

Poniższy przykładowy kod przedstawia użycie DefineConstructor funkcji ustawiania określonego podpisu i atrybutów konstruktora w typie dynamicznym i zwracanie odpowiadającego mu ConstructorBuilder populacji MSIL.

// Define the constructor.
array<Type^>^ constructorArgs = {String::typeid};
ConstructorBuilder^ myConstructorBuilder =
   helloWorldTypeBuilder->DefineConstructor( MethodAttributes::Public,
      CallingConventions::Standard, constructorArgs );
// Generate IL for the method. The constructor stores its argument in the private field.
ILGenerator^ myConstructorIL = myConstructorBuilder->GetILGenerator();
myConstructorIL->Emit( OpCodes::Ldarg_0 );
myConstructorIL->Emit( OpCodes::Ldarg_1 );
myConstructorIL->Emit( OpCodes::Stfld, myGreetingField );
myConstructorIL->Emit( OpCodes::Ret );
// Define the constructor.
Type[] constructorArgs = { typeof(String) };
ConstructorBuilder myConstructorBuilder =
   helloWorldTypeBuilder.DefineConstructor(MethodAttributes.Public,
                      CallingConventions.Standard, constructorArgs);
// Generate IL for the method. The constructor stores its argument in the private field.
ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
myConstructorIL.Emit(OpCodes.Ldarg_0);
myConstructorIL.Emit(OpCodes.Ldarg_1);
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField);
myConstructorIL.Emit(OpCodes.Ret);
' Define the constructor.
Dim constructorArgs As Type() = {GetType(String)}
Dim myConstructorBuilder As ConstructorBuilder = helloWorldTypeBuilder.DefineConstructor _
                     (MethodAttributes.Public, CallingConventions.Standard, constructorArgs)
' Generate IL for the method. The constructor stores its argument in the private field.
Dim myConstructorIL As ILGenerator = myConstructorBuilder.GetILGenerator()
myConstructorIL.Emit(OpCodes.Ldarg_0)
myConstructorIL.Emit(OpCodes.Ldarg_1)
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField)
myConstructorIL.Emit(OpCodes.Ret)

Uwagi

Jeśli nie zdefiniujesz konstruktora dla typu dynamicznego, konstruktor bez parametrów jest dostarczany automatycznie i wywołuje konstruktor bez parametrów klasy bazowej.

Jeśli zdefiniujesz konstruktor dla typu dynamicznego, konstruktor bez parametrów nie zostanie podany. Dostępne są następujące opcje udostępniania konstruktora bez parametrów oprócz zdefiniowanego konstruktora:

  • Jeśli chcesz, aby konstruktor bez parametrów, który po prostu wywołuje konstruktor bez parametrów klasy bazowej, możesz użyć DefineDefaultConstructor metody , aby utworzyć jeden (i opcjonalnie ograniczyć dostęp do niego). Nie udostępniaj implementacji dla tego konstruktora bez parametrów. Jeśli to zrobisz, podczas próby użycia konstruktora zostanie zgłoszony wyjątek. W przypadku wywołania CreateType metody nie jest zgłaszany żaden wyjątek.

  • Jeśli potrzebujesz konstruktora bez parametrów, który wykonuje coś więcej niż po prostu wywołanie konstruktora bez parametrów klasy bazowej lub który wywołuje inny konstruktor klasy bazowej lub wykonuje coś innego w całości, musisz użyć TypeBuilder.DefineConstructor metody , aby go utworzyć i zapewnić własną implementację.

Dotyczy

DefineConstructor(MethodAttributes, CallingConventions, Type[], Type[][], Type[][])

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

Dodaje nowy konstruktor do typu z podanymi atrybutami, podpisem i modyfikatorami niestandardowymi.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineConstructor(System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ requiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[]? parameterTypes, Type[][]? requiredCustomModifiers, Type[][]? optionalCustomModifiers);
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineConstructor (System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type[] parameterTypes, Type[][] requiredCustomModifiers, Type[][] optionalCustomModifiers);
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineConstructor : System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineConstructor (attributes As MethodAttributes, callingConvention As CallingConventions, parameterTypes As Type(), requiredCustomModifiers As Type()(), optionalCustomModifiers As Type()()) As ConstructorBuilder

Parametry

attributes
MethodAttributes

Atrybuty konstruktora.

callingConvention
CallingConventions

Konwencja wywoływania konstruktora.

parameterTypes
Type[]

Typy parametrów konstruktora.

requiredCustomModifiers
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.

optionalCustomModifiers
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

Zdefiniowany konstruktor.

Atrybuty

Wyjątki

Rozmiar requiredCustomModifiers lub optionalCustomModifiers nie jest równy rozmiarowi elementu parameterTypes.

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

-lub-

Dla bieżącego typu IsGenericType dynamicznego właściwość to true, ale IsGenericTypeDefinition właściwość to false.

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