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.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| InvalidOperationException | The enclosing type has not been created. -or- This type is non-abstract and contains an abstract method. -or- This type is not an abstract class or an interface and has a method without a method body. |
| NotSupportedException | The type contains invalid Microsoft intermediate language (MSIL) code. -or- The branch target is specified using a 1-byte offset, but the target is at a distance greater than 127 bytes from the branch. |
| TypeLoadException | 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.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.