Export (0) Print
Expand All
17 out of 43 rated this helpful - Rate this topic

Type Class

Updated: September 2009

Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public abstract class Type : MemberInfo, 
	_Type, IReflect

Type 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, 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.

The C# typeof operator (GetType operator in Visual Basic, typeid operator in Visual C++) returns a Type object.

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.

Reflection can be used to access members (that is, to invoke methods, get and set property values, and so on) without special permissions, as long as the access level of the members would allow similar access by compiled code. To access members that compiled code would not be able to access, such as private members of other types and internal members of other assemblies, ReflectionPermission is required.

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.

NoteNote:

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.

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

  • Classes

  • Value types

  • Arrays

  • Interfaces

  • Pointers

  • Enumerations

  • Constructed generic types and generic type definitions

  • Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions

A reference to the Type object associated with a type can be obtained in the following ways:

  • The Object.GetType method returns a Type object that represents the type of an instance.

  • The static 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 of the public and private types defined in a module. (You can obtain an instance of Module through the Assembly.GetModule or Assembly.GetModules methods, or through the Type.Module property.)

  • 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 Visual Basic).

NoteNote:

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.

NoteNote:

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

Notes to Inheritors:

When you inherit from Type, you must override the following members: GUID, InvokeMember(String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]), Module, Assembly, TypeHandle, FullName, Namespace, AssemblyQualifiedName, BaseType, GetConstructorImpl, GetConstructors(BindingFlags), GetMethodImpl, GetMethods(BindingFlags), GetField(String, BindingFlags), GetFields(BindingFlags), GetInterface(String, Boolean), GetInterfaces(), GetEvent(String, BindingFlags), GetEvents(BindingFlags), GetPropertyImpl, GetProperties(BindingFlags), GetNestedType(String, BindingFlags), GetNestedTypes(BindingFlags), GetAttributeFlagsImpl, IsArrayImpl, IsByRefImpl, IsPointerImpl, IsPrimitiveImpl, IsCOMObjectImpl, GetElementType(), HasElementTypeImpl, UnderlyingSystemType, MemberInfo.Name, MemberInfo.IsDefined, MemberInfo.GetCustomAttributes(Boolean), and GetCustomAttributes.

The following code example shows a few representative features of Type. The C# typeof operator (GetType operator in Visual Basic, typeid operator in Visual C++) is used to get a Type object representing String. From this Type object, the GetMethod method is used to get a MethodInfo representing the Substring overload that takes a starting location and a length.

To identify the overload signature, the code example creates a temporary array containing two Type objects representing int (Integer in Visual Basic).

NoteNote:

To be precise, the array contains two references to the instance of Type that represents int in the current application domain. For any type, there is only one instance of Type per application domain.

The code example uses the MethodInfo to invoke the Substring method on the string "Hello, World!", and displays the result.

using System;
using System.Reflection;

class Example
{
    static void Main()
    {
        Type t = typeof(String);

        MethodInfo substr = t.GetMethod("Substring", 
            new Type[] { typeof(int), typeof(int) });

        Object result = 
            substr.Invoke("Hello, World!", new Object[] { 7, 5 });
        Console.WriteLine("{0} returned \"{1}\".", substr, result);
    }
}

/* This code example produces the following output:

System.String Substring(Int32, Int32) returned "World".
 */

This type is thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

September 2009

Clarified the access that reflection allows to members with various access levels.

Content bug fix.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.