This documentation is archived and is not being maintained.

TypeBuilder.AddInterfaceImplementation Method

Adds an interface that this type implements.

[Visual Basic]
Public Sub AddInterfaceImplementation( _
   ByVal interfaceType As Type _
)
[C#]
public void AddInterfaceImplementation(
 Type interfaceType
);
[C++]
public: void AddInterfaceImplementation(
 Type* interfaceType
);
[JScript]
public function AddInterfaceImplementation(
   interfaceType : Type
);

Parameters

interfaceType
The interface that this type implements.

Exceptions

Exception Type Condition
ArgumentNullException interfaceType is a null reference (Nothing in Visual Basic).
InvalidOperationException The type was previously created using CreateType.

Example

[Visual Basic, C#, C++] The following code sample demonstrates the implementation of an interface on a dynamically created type using AddInterfaceImplementation.

[Visual Basic] 
' 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)

[C#] 
// 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);

[C++] 
// Mark the class as implementing 'IHello' interface.
helloWorldTypeBuilder->AddInterfaceImplementation(__typeof(IHello));
MethodBuilder* myMethodBuilder =
    helloWorldTypeBuilder->DefineMethod(S"SayHello",static_cast<MethodAttributes>(MethodAttributes::Public|MethodAttributes::Virtual),
    0,
    0);
// Generate IL for 'SayHello' method.
ILGenerator* myMethodIL = myMethodBuilder->GetILGenerator();
myMethodIL->EmitWriteLine(myGreetingField);
myMethodIL->Emit(OpCodes::Ret);
MethodInfo* sayHelloMethod = __typeof(IHello)->GetMethod(S"SayHello");
helloWorldTypeBuilder->DefineMethodOverride(myMethodBuilder,sayHelloMethod);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TypeBuilder Class | TypeBuilder Members | System.Reflection.Emit Namespace

Show: