Export (0) Print
Expand All

TypeBuilder.AddInterfaceImplementation Method

Adds an interface that this type implements.

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

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

Parameters

interfaceType
Type: System.Type
The interface that this type implements.

ExceptionCondition
ArgumentNullException

interfaceType is null.

InvalidOperationException

The type was previously created using CreateType.

The following example demonstrates the implementation of an interface on a dynamically created nested type using AddInterfaceImplementation.


using System;
using System.Reflection;
using System.Reflection.Emit;

public interface IMyInterface
{
   string HelloMethod(string parameter);
} 

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      AssemblyName myAssemblyName = new AssemblyName("Example");
      AssemblyBuilder myAssembly = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");

      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType = myModule.DefineType("MyHelloWorld", TypeAttributes.Public);

      // Define a public nested class named 'MyNestedClass' that implements
      // IMyInterface.
      TypeBuilder myNestedClassType = 
         myHelloWorldType.DefineNestedType("MyNestedClass", 
                     TypeAttributes.NestedPublic, typeof(Example), 
                     new Type[] { typeof(IMyInterface) });

      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));

      // Define the HelloMethod of IMyInterface.
      MethodBuilder myHelloMethod = myNestedClassType.DefineMethod("HelloMethod", 
         MethodAttributes.Public | MethodAttributes.Virtual, 
         typeof(string), new Type[] { typeof(string) });

      // Generate IL for the method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi, ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      myMethodIL.Emit(OpCodes.Ldstr, "!");
      myMethodIL.Emit(OpCodes.Call, typeof(string).GetMethod("Concat", 
                         new Type[]{typeof(string), typeof(string), typeof(string)}));
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo = typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement HelloMethod method of IMyInterface.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);

      Type myType = myHelloWorldType.CreateType();

      // Create MyNestedClass type.
      Type myNestedType = myNestedClassType.CreateType();

      // Create an instance of 'MyNestedClass' and cast it to IMyInterface.
      IMyInterface myInterface = (IMyInterface) Activator.CreateInstance(myNestedType);

      // Call HelloMethod.
      outputBlock.Text += myInterface.HelloMethod("Bill") + "\n";
   }
}

/* This code produces the following output:

Hi, Bill!
 */


Silverlight

Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft