TypeBuilder.DefineDefaultConstructor(MethodAttributes) Метод

Определение

Определяет конструктор без параметров. Конструктор, определенный здесь, просто вызывает конструктор без параметров родительского элемента.

public:
 System::Reflection::Emit::ConstructorBuilder ^ DefineDefaultConstructor(System::Reflection::MethodAttributes attributes);
public System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructor(System.Reflection.MethodAttributes attributes);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.Emit.ConstructorBuilder DefineDefaultConstructor(System.Reflection.MethodAttributes attributes);
member this.DefineDefaultConstructor : System.Reflection.MethodAttributes -> System.Reflection.Emit.ConstructorBuilder
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.DefineDefaultConstructor : System.Reflection.MethodAttributes -> System.Reflection.Emit.ConstructorBuilder
Public Function DefineDefaultConstructor (attributes As MethodAttributes) As ConstructorBuilder

Параметры

attributes
MethodAttributes

Объект MethodAttributes , представляющий атрибуты, применяемые к конструктору.

Возвращаемое значение

Возвращает конструктор.

Атрибуты

Исключения

Родительский тип (базовый тип) не имеет конструктора без параметров.

Тип был создан ранее с помощью CreateType().

–или–

Для текущего динамического типа свойство равноIsGenericType, true но IsGenericTypeDefinition свойство равноfalse.

Примеры

В следующем примере кода показано использование DefineConstructor конкретной подписи и атрибутов конструктора для динамического типа и возврата соответствующего ConstructorBuilder для совокупности MSIL.

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

Комментарии

Так как конструктор без параметров определяется автоматически, необходимо вызвать этот метод только в следующих ситуациях:

  • Вы определили другой конструктор, а также требуется конструктор без параметров, который просто вызывает конструктор базового класса.

  • Вы хотите задать атрибуты для конструктора без параметров значение, отличное от PrivateScope, , Public, HideBySigSpecialNameи RTSpecialName.

Применяется к