TypeBuilder.AddInterfaceImplementation(Type) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
加入這個類型所實作的介面。
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)
參數
- interfaceType
- Type
這個類型所實作的介面。
- 屬性
例外狀況
interfaceType
為 null
。
先前使用 CreateType() 建立的類型。
範例
下列程式代碼範例示範如何使用 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)