Freigeben über


TypeBuilder.CreateType Method

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

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function CreateType As Type
[SecuritySafeCriticalAttribute]
public Type CreateType()

Return Value

Type: System.Type
The new object for this class.

Exceptions

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, or it has a field that depends on a nested type that has not yet been created.

Remarks

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 throws a TypeLoadException. This is because the loader cannot determine the size of the enclosing type until the nested type has been completed. The caller should create the nested type first by calling CreateType on the TypeBuilder object that represents the nested type. The example for this topic demonstrates this.

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

Examples

The following example shows how to define a type, AType, that depends on its nested enumeration type, AnEnum, because it has a field of type AnEnum. The CreateType method is called on the nested type before it is called on the enclosing type.

Imports System.Reflection
Imports System.Reflection.Emit

Friend Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim asmName As New AssemblyName("DynamicAssembly")
      Dim ab As AssemblyBuilder = _
         AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
            AssemblyBuilderAccess.Run)
      Dim mb As ModuleBuilder = ab.DefineDynamicModule("DynamicModule")

      ' Define a type named "AType", and a nested enumeration "AnEnum". The 
      ' enumeration has one field, "EnumField1", with a value of 1.
      Dim tb As TypeBuilder = mb.DefineType("AType", TypeAttributes.Public)
      Dim eb As TypeBuilder = tb.DefineNestedType("AnEnum", _
         TypeAttributes.NestedPublic Or TypeAttributes.Sealed, _
         GetType([Enum]))
      eb.DefineField("value__", GetType(Integer), _
         FieldAttributes.Private Or FieldAttributes.SpecialName)
      Dim fb As FieldBuilder = eb.DefineField("EnumField1", eb, _
         FieldAttributes.Public Or FieldAttributes.Literal Or FieldAttributes.Static)
      fb.SetConstant(1)

      ' Defining this field creates a dependency between AType and its nested type,
      ' AnEnum. Because this field is of type AnEnum, the loader cannot determine its 
      ' size until the type AnEnum has been created. Therefore, the creation of type 
      ' AType depends on the creation of its nested type, AnEnum.
      tb.DefineField("MyField", eb, FieldAttributes.Public)

      Try
         ' First create the inner (nested) type, then create the outer (nesting)
         ' type. If you comment out the following line, a TypeLoadException is thrown
         ' when the outer type is created, because the type loader does not know the
         ' size of the field "MyField". 
         Dim tNested As Type = eb.CreateType()

         Dim tNesting As Type = tb.CreateType()

         outputBlock.Text &= _
            String.Format("The declaring type of {0} is {1}." & vbLf, _
               tNested, tNesting)

      Catch ex As Exception
         outputBlock.Text &= ex.GetType().Name & ": " & ex.Message
      End Try

   End Sub 
End Class 

' This example produces output similar to the following:
'
'The declaring type of AType+AnEnum is AType.
using System;
using System.Reflection;
using System.Reflection.Emit;

internal class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      AssemblyName asmName = new AssemblyName("DynamicAssembly");
      AssemblyBuilder ab = 
         AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, 
            AssemblyBuilderAccess.Run);
      ModuleBuilder mb = ab.DefineDynamicModule("DynamicModule");

      // Define a type named "AType", and a nested enumeration "AnEnum". The 
      // enumeration has one field, "EnumField1", with a value of 1.
      TypeBuilder tb = mb.DefineType("AType", TypeAttributes.Public);
      TypeBuilder eb = tb.DefineNestedType("AnEnum", 
         TypeAttributes.NestedPublic | TypeAttributes.Sealed, 
         typeof(Enum));
      eb.DefineField("value__", typeof(int), 
         FieldAttributes.Private | FieldAttributes.SpecialName);
      FieldBuilder fb = eb.DefineField("EnumField1", eb, 
         FieldAttributes.Public | FieldAttributes.Literal | FieldAttributes.Static);
      fb.SetConstant(1);

      // Defining this field creates a dependency between AType and its nested type,
      // AnEnum. Because this field is of type AnEnum, the loader cannot determine its 
      // size until the type AnEnum has been created. Therefore, the creation of type 
      // AType depends on the creation of its nested type, AnEnum.
      tb.DefineField("MyField", eb, FieldAttributes.Public);

      try
      {
         // First create the inner (nested) type, then create the outer (nesting)
         // type. If you comment out the following line, a TypeLoadException is thrown
         // when the outer type is created, because the type loader does not know the
         // size of the field "MyField". 
         Type tNested = eb.CreateType();

         Type tNesting = tb.CreateType();

         outputBlock.Text += 
            String.Format("The declaring type of {0} is {1}.\n", tNested, tNesting);

      }
      catch(Exception ex)
      {
         outputBlock.Text += ex.GetType().Name + ": " + ex.Message;
      }
   }
}

/* This example produces output similar to the following:

The declaring type of AType+AnEnum is AType.
 */

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.