Share via


TypeBuilder.DefineNestedType Method (String, TypeAttributes, Type)

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

Defines a nested type, given its name, attributes, and the type that it extends.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function DefineNestedType ( _
    name As String, _
    attr As TypeAttributes, _
    parent As Type _
) As TypeBuilder
[SecuritySafeCriticalAttribute]
public TypeBuilder DefineNestedType(
    string name,
    TypeAttributes attr,
    Type parent
)

Parameters

  • name
    Type: System.String
    The short name of the type. name cannot contain embedded nulls.
  • parent
    Type: System.Type
    The type that the nested type extends.

Return Value

Type: System.Reflection.Emit.TypeBuilder
The defined nested type.

Exceptions

Exception Condition
ArgumentException

The nested attribute is not specified.

-or-

This type is sealed.

-or-

This type is an array.

-or-

This type is an interface, but the nested type is not an interface.

-or-

The length of name is zero.

ArgumentNullException

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

Remarks

This method can be used to create nested types even after the CreateType method has been called on the enclosing type.

The nested type needs to be complete before you can reflect on it using GetMembers, GetNestedType, or GetNestedTypes.

See the description of CreateType for the order in which nested types and nesting types should be completed.

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.