Edit

Share via


System.Type class

This article provides supplementary remarks to the reference documentation for this API.

The Type class is the root of the System.Reflection functionality and is the primary way to access metadata. Use the members of Type to get information about a type declaration, about the members of a type (such as the constructors, methods, fields, properties, and events of a class), as well as the module and the assembly in which the class is deployed.

No permissions are required for code to use reflection to get information about types and their members, regardless of their access levels. No permissions are required for code to use reflection to access public members, or other members whose access levels would make them visible during normal compilation. However, in order for your code to use reflection to access members that would normally be inaccessible, such as private or internal methods, or protected fields of a type your class does not inherit, your code must have ReflectionPermission. See Security Considerations for Reflection.

Type is an abstract base class that allows multiple implementations. The system will always provide the derived class RuntimeType. In reflection, all classes beginning with the word Runtime are created only once per object in the system and support comparison operations.

Note

In multithreading scenarios, do not lock Type objects in order to synchronize access to static data. Other code, over which you have no control, might also lock your class type. This might result in a deadlock. Instead, synchronize access to static data by locking a private static object.

Note

A derived class can access protected members of the calling code's base classes. Also, access is allowed to assembly members of the calling code's assembly. As a rule, if you are allowed access in early-bound code, then you are also allowed access in late-bound code.

Note

Interfaces that extend other interfaces do not inherit the methods defined in the extended interfaces.

What types does a Type object represent?

This class is thread safe; multiple threads can concurrently read from an instance of this type. An instance of the Type class can represent any of the following types:

  • Classes
  • Value types
  • Arrays
  • Interfaces
  • Enumerations
  • Delegates
  • Constructed generic types and generic type definitions
  • Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions

Retrieve a Type object

The Type object associated with a particular type can be obtained in the following ways:

  • The instance Object.GetType method returns a Type object that represents the type of an instance. Because all managed types derive from Object, the GetType method can be called on an instance of any type.

    The following example calls the Object.GetType method to determine the runtime type of each object in an object array.

    object[] values = { "word", true, 120, 136.34, 'a' };
    foreach (var value in values)
        Console.WriteLine("{0} - type {1}", value,
                          value.GetType().Name);
    
    // The example displays the following output:
    //       word - type String
    //       True - type Boolean
    //       120 - type Int32
    //       136.34 - type Double
    //       a - type Char
    
    let values: obj[] = [| "word"; true; 120; 136.34; 'a' |]
    for value in values do
       printfn $"{value} - type {value.GetType().Name}"
    
    // The example displays the following output:
    //       word - type String
    //       True - type Boolean
    //       120 - type Int32
    //       136.34 - type Double
    //       a - type Char
    
    Module Example1
       Public Sub Main()
          Dim values() As Object = { "word", True, 120, 136.34, "a"c }
          For Each value In values
             Console.WriteLine("{0} - type {1}", value, 
                               value.GetType().Name)
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       word - type String
    '       True - type Boolean
    '       120 - type Int32
    '       136.34 - type Double
    '       a - type Char
    
  • The static Type.GetType methods return a Type object that represents a type specified by its fully qualified name.

  • The Module.GetTypes, Module.GetType, and Module.FindTypes methods return Type objects that represent the types defined in a module. The first method can be used to obtain an array of Type objects for all the public and private types defined in a module. (You can obtain an instance of Module through the Assembly.GetModule or Assembly.GetModules method, or through the Type.Module property.)

  • The System.Reflection.Assembly object contains a number of methods to retrieve the classes defined in an assembly, including Assembly.GetType, Assembly.GetTypes, and Assembly.GetExportedTypes.

  • The FindInterfaces method returns a filtered list of interface types supported by a type.

  • The GetElementType method returns a Type object that represents the element.

  • The GetInterfaces and GetInterface methods return Type objects representing the interface types supported by a type.

  • The GetTypeArray method returns an array of Type objects representing the types specified by an arbitrary set of objects. The objects are specified with an array of type Object.

  • The GetTypeFromProgID and GetTypeFromCLSID methods are provided for COM interoperability. They return a Type object that represents the type specified by a ProgID or CLSID.

  • The GetTypeFromHandle method is provided for interoperability. It returns a Type object that represents the type specified by a class handle.

  • The C# typeof operator, the C++ typeid operator, and the Visual Basic GetType operator obtain the Type object for a type.

  • The MakeGenericType method returns a Type object representing a constructed generic type, which is an open constructed type if its ContainsGenericParameters property returns true, and a closed constructed type otherwise. A generic type can be instantiated only if it is closed.

  • The MakeArrayType, MakePointerType, and MakeByRefType methods return Type objects that represent, respectively, an array of a specified type, a pointer to a specified type, and the type of a reference parameter (ref in C#, 'byref' in F#, ByRef in Visual Basic).

Compare type objects for equality

A Type object that represents a type is unique; that is, two Type object references refer to the same object if and only if they represent the same type. This allows for comparison of Type objects using reference equality. The following example compares the Type objects that represent a number of integer values to determine whether they are of the same type.

long number1 = 1635429;
int number2 = 16203;
double number3 = 1639.41;
long number4 = 193685412;

// Get the type of number1.
Type t = number1.GetType();

// Compare types of all objects with number1.
Console.WriteLine("Type of number1 and number2 are equal: {0}",
                  Object.ReferenceEquals(t, number2.GetType()));
Console.WriteLine("Type of number1 and number3 are equal: {0}",
                  Object.ReferenceEquals(t, number3.GetType()));
Console.WriteLine("Type of number1 and number4 are equal: {0}",
                  Object.ReferenceEquals(t, number4.GetType()));

// The example displays the following output:
//       Type of number1 and number2 are equal: False
//       Type of number1 and number3 are equal: False
//       Type of number1 and number4 are equal: True
let number1 = 1635429L
let number2 = 16203
let number3 = 1639.41
let number4 = 193685412L

// Get the type of number1.
let t = number1.GetType()

// Compare types of all objects with number1.
printfn $"Type of number1 and number2 are equal: {Object.ReferenceEquals(t, number2.GetType())}"
printfn $"Type of number1 and number3 are equal: {Object.ReferenceEquals(t, number3.GetType())}"
printfn $"Type of number1 and number4 are equal: {Object.ReferenceEquals(t, number4.GetType())}"

// The example displays the following output:
//       Type of number1 and number2 are equal: False
//       Type of number1 and number3 are equal: False
//       Type of number1 and number4 are equal: True
Module Example
   Public Sub Main()
      Dim number1 As Long = 1635429
      Dim number2 As Integer = 16203
      Dim number3 As Double = 1639.41
      Dim number4 As Long = 193685412
      
      ' Get the type of number1.
      Dim t As Type = number1.GetType()
      
      ' Compare types of all objects with number1.
      Console.WriteLine("Type of number1 and number2 are equal: {0}",
                        Object.ReferenceEquals(t, number2.GetType()))
      Console.WriteLine("Type of number1 and number3 are equal: {0}",
                        Object.ReferenceEquals(t, number3.GetType()))
      Console.WriteLine("Type of number1 and number4 are equal: {0}",
                        Object.ReferenceEquals(t, number4.GetType()))
   End Sub
End Module
' The example displays the following output:
'       Type of number1 and number2 are equal: False
'       Type of number1 and number3 are equal: False
'       Type of number1 and number4 are equal: True