共用方式為


TypeBuilder.AddInterfaceImplementation Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Adds an interface that this type implements.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<SecuritySafeCriticalAttribute> _
Public Sub AddInterfaceImplementation ( _
    interfaceType As Type _
)
[ComVisibleAttribute(true)]
[SecuritySafeCriticalAttribute]
public void AddInterfaceImplementation(
    Type interfaceType
)

Parameters

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

Exceptions

Exception Condition
ArgumentNullException

interfaceType is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The type was previously created using CreateType.

Examples

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

Imports System.Reflection
Imports System.Reflection.Emit

Public Interface IMyInterface
   Function HelloMethod(ByVal parameter As String) As String
End Interface 

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim myAssemblyName As New AssemblyName("Example")
      Dim myAssembly As AssemblyBuilder = _
               AppDomain.CurrentDomain.DefineDynamicAssembly(myAssemblyName, _
                                                             AssemblyBuilderAccess.Run)
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")

      ' Define a public class named "MyHelloWorld".
      Dim myHelloWorldType As TypeBuilder = _
               myModule.DefineType("MyHelloWorld", TypeAttributes.Public)

      ' Define a public nested class named 'MyNestedClass' that implements
      ' IMyInterface.
      Dim myNestedClassType As TypeBuilder = _
               myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
               GetType(Example), New Type() { GetType(IMyInterface) })

      ' Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))

      ' Define the HelloMethod of IMyInterface.
      Dim myHelloMethod As MethodBuilder = _
               myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
               MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})

      ' Generate IL for the method.
      Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
      myMethodIL.Emit(OpCodes.Ldstr, "Hi, ")
      myMethodIL.Emit(OpCodes.Ldarg_1)
      myMethodIL.Emit(OpCodes.Ldstr, "!")
      myMethodIL.Emit(OpCodes.Call, GetType(String).GetMethod("Concat", _
                                New Type() {GetType(String), GetType(String), GetType(String)}))
      myMethodIL.Emit(OpCodes.Ret)

      Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
      ' Implement HelloMethod method of IMyInterface.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)

      Dim myType As Type = myHelloWorldType.CreateType()

      ' Create MyNestedClass type.
      Dim myNestedType As Type = myNestedClassType.CreateType()

      ' Create an instance of 'MyNestedClass' and cast it to IMyInterface.
      Dim myInterface As IMyInterface = _
            CType(Activator.CreateInstance(myNestedType), IMyInterface)

      ' Call HelloMethod.
      outputBlock.Text &= myInterface.HelloMethod("Bill") & vbCrLf

   End Sub
End Class 

' This code produces the following output:
'
'Hi, Bill!
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!
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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