TypeBuilder::CreateType Method ()


Creates a Type object for the class. After defining fields and methods on the class, CreateType is called in order to load its Type object.

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

Type^ CreateType()

Return Value

Type: System::Type^

Returns the new Type object for this class.

Exception Condition

The enclosing type has not been created.


This type is non-abstract and contains an abstract method.


This type is not an abstract class or an interface and has a method without a method body.


The type contains invalid Microsoft intermediate language (MSIL) code.


The branch target is specified using a 1-byte offset, but the target is at a distance greater than 127 bytes from the branch.


The type cannot be loaded. For example, it contains a static method that has the calling convention HasThis.

If this type is a nested type, the CreateType method must be called on the enclosing type before it is called on the nested type.

If the current type derives from an incomplete type or implements incomplete interfaces, call the CreateType method on the parent type and the interface types before calling it on the current type.

If the enclosing type contains a field that is a value type defined as a nested type (for example, a field that is an enumeration defined as a nested type), calling the CreateType method on the enclosing type will generate a AppDomain::TypeResolve event. This is because the loader cannot determine the size of the enclosing type until the nested type has been completed. The caller should define a handler for the TypeResolve event to complete the definition of the nested type by calling CreateType on the TypeBuilder object that represents the nested type. The code example for this topic shows how to define such an event handler.

A type is created only once, no matter how many times the CreateType method is called. All calls return the same Type object.

The following code example shows how to define an event handler for the AppDomain::TypeResolve event, in order to call the CreateType method on a nested type during a CreateType call on the enclosing type.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Threading;
using namespace System::Text;
using namespace System::Resources;
using namespace System::Collections;
using namespace System::IO;

// Helper class called when a resolve type event is raised.
ref class TypeResolveHandler
   Module^ m_Module;

   TypeResolveHandler( Module^ mod )
      m_Module = mod;

   Assembly^ ResolveEvent( Object^ sender, ResolveEventArgs^ args );

ref class NestedEnum
   static TypeBuilder^ enumType = nullptr;
   static Type^ tNested = nullptr;
   static Type^ tNesting = nullptr;

   static void Main()
      AssemblyName^ asmName = gcnew AssemblyName;
      asmName->Name = "NestedEnum";
      AssemblyBuilder^ asmBuild = Thread::GetDomain()->DefineDynamicAssembly( asmName, AssemblyBuilderAccess::RunAndSave );
      ModuleBuilder^ modBuild = asmBuild->DefineDynamicModule( "ModuleOne", "NestedEnum.dll" );

      // Hook up the event listening.
      TypeResolveHandler^ typeResolveHandler = gcnew TypeResolveHandler( modBuild );

      // Add a listener for the type resolve events.
      AppDomain^ currentDomain = Thread::GetDomain();
      ResolveEventHandler^ resolveHandler = gcnew ResolveEventHandler( typeResolveHandler, &TypeResolveHandler::ResolveEvent );
      currentDomain->TypeResolve += resolveHandler;
      TypeBuilder^ tb = modBuild->DefineType( "AType", TypeAttributes::Public );
      TypeBuilder^ eb = tb->DefineNestedType( "AnEnum", static_cast<TypeAttributes>(TypeAttributes::NestedPublic | TypeAttributes::Sealed), Enum::typeid, 0 );
      eb->DefineField( "value__", int::typeid, static_cast<FieldAttributes>(FieldAttributes::Private | FieldAttributes::SpecialName) );
      FieldBuilder^ fb = eb->DefineField( "Field1", eb, static_cast<FieldAttributes>(FieldAttributes::Public | FieldAttributes::Literal | FieldAttributes::Static) );
      fb->SetConstant( 1 );
      enumType = eb;

      // Comment out this field.
      // When this field is defined, the loader cannot determine the size
      // of the type. Therefore, a TypeResolve event is generated when the
      // nested type is completed.
      tb->DefineField( "Field2", eb, FieldAttributes::Public );
      tNesting = tb->CreateType();
      if ( tNesting == nullptr )
            Console::WriteLine( "NestingType CreateType failed but didn't throw!" );

         tNested = eb->CreateType();
         if ( tNested == nullptr )
                  Console::WriteLine( "NestedType CreateType failed but didn't throw!" );
      catch ( Exception^ ) 

         // This is needed because you might have already completed the type in the TypeResolve event.

      if ( tNested != nullptr )
         Type^ x = tNested->DeclaringType;
         if ( x == nullptr )
                  Console::WriteLine( "Declaring type is null." );
                  Console::WriteLine( x->Name );

      asmBuild->Save( "NestedEnum.dll" );

      // Remove the listener for the type resolve events.
      currentDomain->TypeResolve -= resolveHandler;


Assembly^ TypeResolveHandler::ResolveEvent( Object^ sender, ResolveEventArgs^ args )
   Console::WriteLine( args->Name );

   // Use args.Name to look up the type name. In this case, you are getting AnEnum.
      NestedEnum::tNested = NestedEnum::enumType->CreateType();
   catch ( Exception^ ) 

      // This is needed to throw away InvalidOperationException.
      // Loader might send the TypeResolve event more than once
      // and the type might be complete already.

   // Complete the type.
   return m_Module->Assembly;

int main()

.NET Framework
Available since 1.1
Available since 2.0
Return to top