Export (0) Print
Expand All
3 out of 11 rated this helpful - Rate this topic

Type Class

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
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ 
public abstract class Type extends MemberInfo implements _Type, IReflect
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.None) 
public abstract class Type extends MemberInfo implements _Type, IReflect
Not applicable.

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.

Without ReflectionPermission, code can access only the public members of loaded assemblies. This includes, but is not limited to, unrestricted access to Object.GetType, access to public exported types through Type.GetType, and access to GetTypeFromHandle. Some properties of Type, such as FullName and Attributes, are accessible without ReflectionPermission.

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, 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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.