TypeBuilder.DefineDefaultConstructor(MethodAttributes) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義無參數建構函式。 這裡所定義建構函式只會呼叫父代的無參數建構函式。
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
物件。
傳回
傳回建構函式。
- 屬性
例外狀況
父類型 (基底類型) 沒有無參數建構函式。
範例
下列程式代碼範例示範如何使用 DefineConstructor
在動態類型上設定建構函式的特定簽章和屬性,並傳回對應 ConstructorBuilder 至 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)
備註
由於會自動定義無參數建構函式,因此只有在下列情況下才需要呼叫此方法:
您已定義另一個建構函式,而且也想要只呼叫基類建構函式的無參數建構函式。
您要將無參數建構函式上的屬性設定為、Public、 HideBySigSpecialName和 RTSpecialName以外的PrivateScope專案。