TypeBuilder.AddInterfaceImplementation Method (Type)

 

Adds an interface that this type implements.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)]
public void AddInterfaceImplementation(
	Type interfaceType
)

Parameters

interfaceType
Type: System.Type

The interface that this type implements.

Exception Condition
ArgumentNullException

interfaceType is null.

InvalidOperationException

The type was previously created using CreateType.

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(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);

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Return to top
Show: