Export (0) Print
Expand All

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)

[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.
attr
Type: System.Reflection.TypeAttributes
The attributes of the type.
parent
Type: System.Type
The type that the nested type extends.

Return Value

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

ExceptionCondition
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 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 = 
         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