TypeBuilder.DefineConstructor 메서드

정의

동적 형식에 새 생성자를 추가합니다.

오버로드

DefineConstructor(MethodAttributes, CallingConventions, Type[])

지정된 특성 및 서명을 사용하여 새 생성자를 형식에 추가합니다.

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

지정된 특성, 서명 및 사용자 지정 수정자를 사용하여 새 생성자를 형식에 추가합니다.

DefineConstructor(MethodAttributes, CallingConventions, Type[])

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

지정된 특성 및 서명을 사용하여 새 생성자를 형식에 추가합니다.

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

매개 변수

attributes
MethodAttributes

생성자의 특성입니다.

callingConvention
CallingConventions

생성자의 호출 규칙입니다.

parameterTypes
Type[]

생성자의 매개 변수 형식입니다.

반환

정의된 생성자입니다.

특성

예외

CreateType()을 사용하여 이전에 형식을 만들었습니다.

예제

다음 코드 샘플에서는 를 사용하여 DefineConstructor 동적 형식에서 생성자의 특정 서명 및 특성을 설정하고 MSIL 모집단에 해당하는 를 반환하는 ConstructorBuilder 방법을 보여 줍니다.

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

설명

동적 형식에 대한 생성자를 정의하지 않으면 매개 변수가 없는 생성자가 자동으로 제공되고 기본 클래스의 매개 변수가 없는 생성자를 호출합니다.

동적 형식에 대한 생성자를 정의하는 경우 매개 변수가 없는 생성자가 제공되지 않습니다. 정의한 생성자 외에도 매개 변수가 없는 생성자를 제공하기 위한 다음 옵션이 있습니다.

  • 기본 클래스의 매개 변수 없는 생성자를 호출하는 매개 변수가 없는 생성자를 원하는 경우 메서드를 사용하여 DefineDefaultConstructor 생성하고 필요에 따라 액세스를 제한할 수 있습니다. 이 매개 변수가 없는 생성자에 대한 구현을 제공하지 마세요. 이렇게 하면 생성자를 사용하려고 할 때 예외가 throw됩니다. 메서드가 호출되면 예외가 CreateType throw되지 않습니다.

  • 단순히 기본 클래스의 매개 변수 없는 생성자를 호출하는 것 이상의 작업을 수행하거나 기본 클래스의 다른 생성자를 호출하거나 완전히 다른 작업을 수행하는 매개 변수 없는 생성자를 원하는 경우 메서드를 사용하여 TypeBuilder.DefineConstructor 생성하고 고유한 구현을 제공해야 합니다.

적용 대상

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

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

지정된 특성, 서명 및 사용자 지정 수정자를 사용하여 새 생성자를 형식에 추가합니다.

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

매개 변수

attributes
MethodAttributes

생성자의 특성입니다.

callingConvention
CallingConventions

생성자의 호출 규칙입니다.

parameterTypes
Type[]

생성자의 매개 변수 형식입니다.

requiredCustomModifiers
Type[][]

형식 배열의 배열입니다. 각 형식의 배열은 해당 매개 변수에 필요한 사용자 지정 한정자를 나타냅니다(예: IsConst). 특정 매개 변수에 필수 사용자 지정 한정자가 없는 경우 형식 배열 대신 null을 지정합니다. 매개 변수에 필수 사용자 지정 한정자가 없는 경우 형식 배열 대신 null을 지정합니다.

optionalCustomModifiers
Type[][]

형식 배열의 배열입니다. 각 형식의 배열은 해당 매개 변수의 선택적 사용자 지정 한정자를 나타냅니다(예: IsConst). 특정 매개 변수에 선택적 사용자 지정 한정자가 없는 경우 형식 배열 대신 null을 지정합니다. 매개 변수에 선택적 사용자 지정 한정자가 없는 경우 형식 배열 대신 null을 지정합니다.

반환

정의된 생성자입니다.

특성

예외

requiredCustomModifiers 또는 optionalCustomModifiers의 크기가 parameterTypes의 크기와 같지 않습니다.

CreateType()을 사용하여 이전에 형식을 만들었습니다.

또는

현재 동적 형식에 대해 IsGenericType 속성은 true지만 IsGenericTypeDefinition 속성은 false입니다.

설명

이 오버로드는 관리되는 컴파일러의 디자이너에 대해 제공됩니다.

참고

사용자 지정 한정자에 대한 자세한 내용은 ECMA C# 및 공용 언어 인프라 표준표준 ECMA-335 - CLI(공용 언어 인프라)를 참조하세요.

적용 대상