Share via


TypeBuilder.AddInterfaceImplementation(Type) Metode

Definisi

Menambahkan antarmuka yang diterapkan jenis ini.

public:
 void AddInterfaceImplementation(Type ^ interfaceType);
public void AddInterfaceImplementation (Type interfaceType);
[System.Runtime.InteropServices.ComVisible(true)]
public void AddInterfaceImplementation (Type interfaceType);
member this.AddInterfaceImplementation : Type -> unit
[<System.Runtime.InteropServices.ComVisible(true)>]
member this.AddInterfaceImplementation : Type -> unit
Public Sub AddInterfaceImplementation (interfaceType As Type)

Parameter

interfaceType
Type

Antarmuka yang diterapkan jenis ini.

Atribut

Pengecualian

interfaceTypeadalah null.

Jenis sebelumnya dibuat menggunakan CreateType().

Contoh

Sampel kode berikut menunjukkan implementasi antarmuka pada jenis yang dibuat secara dinamis menggunakan AddInterfaceImplementation.

// Mark the class as implementing 'IHello' interface.
helloWorldTypeBuilder->AddInterfaceImplementation( IHello::typeid );
MethodBuilder^ myMethodBuilder =
   helloWorldTypeBuilder->DefineMethod( "SayHello",
      (MethodAttributes)(MethodAttributes::Public | MethodAttributes::Virtual),
      nullptr,
      nullptr );
// Generate IL for 'SayHello' method.
ILGenerator^ myMethodIL = myMethodBuilder->GetILGenerator();
myMethodIL->EmitWriteLine( myGreetingField );
myMethodIL->Emit( OpCodes::Ret );
MethodInfo^ sayHelloMethod = IHello::typeid->GetMethod( "SayHello" );
helloWorldTypeBuilder->DefineMethodOverride( myMethodBuilder, sayHelloMethod );
 // Mark the class as implementing 'IHello' interface.
 helloWorldTypeBuilder.AddInterfaceImplementation(typeof(IHello));
 MethodBuilder myMethodBuilder =
    helloWorldTypeBuilder.DefineMethod("SayHello",
                         MethodAttributes.Public|MethodAttributes.Virtual,
                         null,
                         null);
 // Generate IL for 'SayHello' method.
 ILGenerator myMethodIL = myMethodBuilder.GetILGenerator();
 myMethodIL.EmitWriteLine(myGreetingField);
 myMethodIL.Emit(OpCodes.Ret);
MethodInfo sayHelloMethod = typeof(IHello).GetMethod("SayHello");
helloWorldTypeBuilder.DefineMethodOverride(myMethodBuilder,sayHelloMethod);
' Mark the class as implementing 'IHello' interface.
helloWorldTypeBuilder.AddInterfaceImplementation(GetType(IHello))
Dim myMethodBuilder As MethodBuilder = helloWorldTypeBuilder.DefineMethod("SayHello", _
                     MethodAttributes.Public Or MethodAttributes.Virtual, Nothing, Nothing)
' Generate IL for 'SayHello' method.
Dim myMethodIL As ILGenerator = myMethodBuilder.GetILGenerator()
myMethodIL.EmitWriteLine(myGreetingField)
myMethodIL.Emit(OpCodes.Ret)
Dim sayHelloMethod As MethodInfo = GetType(IHello).GetMethod("SayHello")
helloWorldTypeBuilder.DefineMethodOverride(myMethodBuilder, sayHelloMethod)

Berlaku untuk