TypeBuilder.AddInterfaceImplementation(Type) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Adds an interface that this type implements.
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)
Parameters
- interfaceType
- Type
The interface that this type implements.
- Attributes
Exceptions
interfaceType
is null
.
The type was previously created using CreateType().
Examples
The following code sample demonstrates the implementation of an interface on a dynamically created type using 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)
Applies to
Samarbejd med os på GitHub
Kilden til dette indhold kan findes på GitHub, hvor du også kan oprette og gennemse problemer og pullanmodninger. Du kan få flere oplysninger i vores vejledning til bidragydere.
Feedback
https://aka.ms/ContentUserFeedback.
Kommer snart: I hele 2024 udfaser vi GitHub-problemer som feedbackmekanisme for indhold og erstatter det med et nyt feedbacksystem. Du kan få flere oplysninger under:Indsend og få vist feedback om