TypeBuilder.DefineNestedType Method (String, TypeAttributes, Type)

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

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

public TypeBuilder DefineNestedType(
	string name,
	TypeAttributes attr,
	Type parent


Type: System.String
The short name of the type. name cannot contain embedded nulls.
Type: System.Reflection.TypeAttributes
The attributes of the type.
Type: System.Type
The type that the nested type extends.

Return Value

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


The nested attribute is not specified.


This type is sealed.


This type is an array.


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


The length of name is zero.


name is null.

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.

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 = 
                     TypeAttributes.NestedPublic, typeof(Example), 
                     new Type[] { typeof(IMyInterface) });

      // Implement 'IMyInterface' interface.

      // 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.Ldstr, "!");
      myMethodIL.Emit(OpCodes.Call, typeof(string).GetMethod("Concat", 
                         new Type[]{typeof(string), typeof(string), typeof(string)}));

      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!


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