Type Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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.
Inheritance Hierarchy
System.Object
System.Reflection.MemberInfo
System.Type
System.Reflection.Emit.EnumBuilder
System.Reflection.Emit.GenericTypeParameterBuilder
System.Reflection.Emit.TypeBuilder
System.Reflection.TypeDelegator
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public MustInherit Class Type _
Inherits MemberInfo _
Implements IReflect
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public abstract class Type : MemberInfo,
IReflect
The Type type exposes the following members.
Properties
Name | Description | |
---|---|---|
Assembly | Gets the Assembly in which the type is declared. For generic types, gets the Assembly in which the generic type is defined. | |
AssemblyQualifiedName | Gets the assembly-qualified name of the Type, which includes the name of the assembly from which the Type was loaded. | |
Attributes | Gets the attributes associated with the Type. | |
BaseType | Gets the type from which the current Type directly inherits. | |
ContainsGenericParameters | Gets a value indicating whether the current Type object has type parameters that have not been replaced by specific types. | |
DeclaringMethod | Gets a MethodBase that represents the declaring method, if the current Type represents a type parameter of a generic method. | |
DeclaringType | Gets the type that declares the current nested type or generic type parameter. (Overrides MemberInfo.DeclaringType.) | |
DefaultBinder | Gets a reference to the default binder, which implements internal rules for selecting the appropriate members to be called by InvokeMember. | |
FullName | Gets the fully qualified name of the Type, including the namespace of the Type but not the assembly. | |
GenericParameterAttributes | Gets a combination of GenericParameterAttributes flags that describe the covariance and special constraints of the current generic type parameter. | |
GenericParameterPosition | Gets the position of the type parameter in the type parameter list of the generic type or method that declared the parameter, when the Type object represents a type parameter of a generic type or a generic method. | |
GUID | Gets the GUID associated with the Type. | |
HasElementType | Gets a value indicating whether the current Type encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference. | |
IsAbstract | Gets a value indicating whether the Type is abstract and must be overridden. | |
IsAnsiClass | Gets a value indicating whether the string format attribute AnsiClass is selected for the Type. | |
IsArray | Gets a value indicating whether the Type is an array. | |
IsAutoClass | Gets a value indicating whether the string format attribute AutoClass is selected for the Type. | |
IsAutoLayout | Gets a value indicating whether the class layout attribute AutoLayout is selected for the Type. | |
IsByRef | Gets a value indicating whether the Type is passed by reference. | |
IsClass | Gets a value indicating whether the Type is a class; that is, not a value type or interface. | |
IsCOMObject | Gets a value indicating whether the Type is a COM object. | |
IsEnum | Gets a value indicating whether the current Type represents an enumeration. | |
IsGenericParameter | Gets a value indicating whether the current Type represents a type parameter in the definition of a generic type or method. | |
IsGenericType | Gets a value indicating whether the current type is a generic type. | |
IsGenericTypeDefinition | Gets a value indicating whether the current Type represents a generic type definition, from which other generic types can be constructed. | |
IsImport | Gets a value indicating whether the Type has a ComImportAttribute attribute applied, indicating that it was imported from a COM type library. | |
IsInterface | Gets a value indicating whether the Type is an interface; that is, not a class or a value type. | |
IsNested | Gets a value indicating whether the current Type object represents a type whose definition is nested inside the definition of another type. | |
IsNestedAssembly | Gets a value indicating whether the Type is nested and visible only within its own assembly. | |
IsNestedFamANDAssem | Gets a value indicating whether the Type is nested and visible only to classes that belong to both its own family and its own assembly. | |
IsNestedFamily | Gets a value indicating whether the Type is nested and visible only within its own family. | |
IsNestedFamORAssem | Gets a value indicating whether the Type is nested and visible only to classes that belong to either its own family or to its own assembly. | |
IsNestedPrivate | Gets a value indicating whether the Type is nested and declared private. | |
IsNestedPublic | Gets a value indicating whether a class is nested and declared public. | |
IsNotPublic | Gets a value indicating whether the Type is not declared public. | |
IsPointer | Gets a value indicating whether the Type is a pointer. | |
IsPrimitive | Gets a value indicating whether the Type is one of the primitive types. | |
IsPublic | Gets a value indicating whether the Type is declared public. | |
IsSealed | Gets a value indicating whether the Type is declared sealed. | |
IsSpecialName | Gets a value indicating whether the Type has a name that requires special handling. | |
IsUnicodeClass | Gets a value indicating whether the string format attribute UnicodeClass is selected for the Type. | |
IsValueType | Gets a value indicating whether the Type is a value type. | |
IsVisible | Gets a value indicating whether the Type can be accessed by code outside the assembly. | |
MemberType | Gets a MemberTypes value indicating that this member is a type or a nested type. (Overrides MemberInfo.MemberType.) | |
MetadataToken | Gets a value that identifies a metadata element. (Inherited from MemberInfo.) | |
Module | Gets the module (the DLL) in which the current Type is defined. | |
Name | Gets the name of the current member. (Inherited from MemberInfo.) | |
Namespace | Gets the namespace of the Type. | |
ReflectedType | Gets the class object that was used to obtain this member. (Overrides MemberInfo.ReflectedType.) | |
TypeHandle | Gets the handle for the current Type. | |
UnderlyingSystemType | Indicates the type provided by the common language runtime that represents this type. |
Top
Methods
Name | Description | |
---|---|---|
Equals(Object) | Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Object. (Overrides Object.Equals(Object).) | |
Equals(Type) | Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Type. | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
FindMembers | Returns a filtered array of MemberInfo objects of the specified member type. | |
GetArrayRank | Gets the number of dimensions in an Array. | |
GetAttributeFlagsImpl | When overridden in a derived class, implements the Attributes property and gets a bitmask indicating the attributes associated with the Type. | |
GetConstructor(array<Type[]) | Searches for a public instance constructor whose parameters match the types in the specified array. | |
GetConstructor(BindingFlags, Binder, array<Type[], array<ParameterModifier[]) | Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints. | |
GetConstructorImpl | When overridden in a derived class, searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention. | |
GetConstructors() | Returns all the public constructors defined for the current Type. | |
GetConstructors(BindingFlags) | When overridden in a derived class, searches for the constructors defined for the current Type, using the specified BindingFlags. | |
GetCustomAttributes(Boolean) | When overridden in a derived class, returns an array of all custom attributes applied to this member. (Inherited from MemberInfo.) | |
GetCustomAttributes(Type, Boolean) | When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type. (Inherited from MemberInfo.) | |
GetDefaultMembers | Searches for the members defined for the current Type whose DefaultMemberAttribute is set. | |
GetElementType | When overridden in a derived class, returns the Type of the object encompassed or referred to by the current array, pointer or reference type. | |
GetEvent(String) | Returns the EventInfo object representing the specified public event. | |
GetEvent(String, BindingFlags) | When overridden in a derived class, returns the EventInfo object representing the specified event, using the specified binding constraints. | |
GetEvents() | Returns all the public events that are declared or inherited by the current Type. | |
GetEvents(BindingFlags) | When overridden in a derived class, searches for events that are declared or inherited by the current Type, using the specified binding constraints. | |
GetField(String) | Searches for the public field with the specified name. | |
GetField(String, BindingFlags) | Searches for the specified field, using the specified binding constraints. | |
GetFields() | Returns all the public fields of the current Type. | |
GetFields(BindingFlags) | When overridden in a derived class, searches for the fields defined for the current Type, using the specified binding constraints. | |
GetGenericArguments | Returns an array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition. | |
GetGenericParameterConstraints | Returns an array of Type objects that represent the constraints on the current generic type parameter. | |
GetGenericTypeDefinition | Returns a Type object that represents a generic type definition from which the current generic type can be constructed. | |
GetHashCode | Returns the hash code for this instance. (Overrides Object.GetHashCode().) | |
GetInterface | When overridden in a derived class, searches for the specified interface, specifying whether to do a case-insensitive search for the interface name. | |
GetInterfaceMap | Returns an interface mapping for the specified interface type. | |
GetInterfaces | When overridden in a derived class, gets all the interfaces implemented or inherited by the current Type. | |
GetMember(String) | Searches for the public members with the specified name. | |
GetMember(String, BindingFlags) | Searches for the specified members, using the specified binding constraints. | |
GetMember(String, MemberTypes, BindingFlags) | Searches for the specified members of the specified member type, using the specified binding constraints. | |
GetMembers() | Returns all the public members of the current Type. | |
GetMembers(BindingFlags) | When overridden in a derived class, searches for the members defined for the current Type, using the specified binding constraints. | |
GetMethod(String) | Searches for the public method with the specified name. | |
GetMethod(String, BindingFlags) | Searches for the specified method, using the specified binding constraints. | |
GetMethod(String, array<Type[]) | Searches for the specified public method whose parameters match the specified argument types. | |
GetMethod(String, array<Type[], array<ParameterModifier[]) | Searches for the specified public method whose parameters match the specified argument types and modifiers. | |
GetMethod(String, BindingFlags, Binder, array<Type[], array<ParameterModifier[]) | Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints. | |
GetMethod(String, BindingFlags, Binder, CallingConventions, array<Type[], array<ParameterModifier[]) | Searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention. | |
GetMethodImpl | When overridden in a derived class, searches for the specified method whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention. | |
GetMethods() | Returns all the public methods of the current Type. | |
GetMethods(BindingFlags) | When overridden in a derived class, searches for the methods defined for the current Type, using the specified binding constraints. | |
GetNestedType | When overridden in a derived class, searches for the specified nested type, using the specified binding constraints. | |
GetNestedTypes | When overridden in a derived class, searches for the types nested in the current Type, using the specified binding constraints. | |
GetProperties() | Returns all the public properties of the current Type. | |
GetProperties(BindingFlags) | When overridden in a derived class, searches for the properties of the current Type, using the specified binding constraints. | |
GetProperty(String) | Searches for the public property with the specified name. | |
GetProperty(String, BindingFlags) | Searches for the specified property, using the specified binding constraints. | |
GetProperty(String, Type) | Searches for the public property with the specified name and return type. | |
GetProperty(String, Type, array<Type[]) | Searches for the specified public property whose parameters match the specified argument types. | |
GetProperty(String, Type, array<Type[], array<ParameterModifier[]) | Searches for the specified public property whose parameters match the specified argument types and modifiers. | |
GetProperty(String, BindingFlags, Binder, Type, array<Type[], array<ParameterModifier[]) | Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints. | |
GetPropertyImpl | When overridden in a derived class, searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints. | |
GetType() | Gets the current Type. | |
GetType(String) | Gets the Type with the specified name, performing a case-sensitive search. | |
GetType(String, Boolean) | Gets the Type with the specified name, performing a case-sensitive search and specifying whether to throw an exception if the type is not found. | |
GetType(String, Boolean, Boolean) | Gets the Type with the specified name, specifying whether to perform a case-sensitive search and whether to throw an exception if the type is not found. | |
GetTypeCode | Gets the underlying type code of the specified Type. | |
GetTypeFromHandle | Gets the type referenced by the specified type handle. | |
GetTypeHandle | Gets the handle for the Type of a specified object. | |
HasElementTypeImpl | When overridden in a derived class, implements the HasElementType property and determines whether the current Type encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference. | |
InvokeMember(String, BindingFlags, Binder, Object, array<Object[]) | Invokes the specified member, using the specified binding constraints and matching the specified argument list. | |
InvokeMember(String, BindingFlags, Binder, Object, array<Object[], array<ParameterModifier[], CultureInfo, array<String[]) | When overridden in a derived class, invokes the specified member, using the specified binding constraints and matching the specified argument list, modifiers and culture. | |
IsArrayImpl | When overridden in a derived class, implements the IsArray property and determines whether the Type is an array. | |
IsAssignableFrom | Determines whether an instance of the current Type can be assigned from an instance of the specified Type. | |
IsByRefImpl | When overridden in a derived class, implements the IsByRef property and determines whether the Type is passed by reference. | |
IsCOMObjectImpl | When overridden in a derived class, implements the IsCOMObject property and determines whether the Type is a COM object. | |
IsDefined | When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member. (Inherited from MemberInfo.) | |
IsInstanceOfType | Determines whether the specified object is an instance of the current Type. | |
IsPointerImpl | When overridden in a derived class, implements the IsPointer property and determines whether the Type is a pointer. | |
IsPrimitiveImpl | When overridden in a derived class, implements the IsPrimitive property and determines whether the Type is one of the primitive types. | |
IsSubclassOf | Determines whether the class represented by the current Type derives from the class represented by the specified Type. | |
IsValueTypeImpl | Implements the IsValueType property and determines whether the Type is a value type; that is, not a class or an interface. | |
MakeArrayType() | Returns a Type object representing a one-dimensional array of the current type, with a lower bound of zero. | |
MakeArrayType(Int32) | Returns a Type object representing an array of the current type, with the specified number of dimensions. | |
MakeByRefType | Returns a Type object that represents the current type when passed as a ref parameter (ByRef parameter in Visual Basic). | |
MakeGenericType | Substitutes the elements of an array of types for the type parameters of the current generic type definition and returns a Type object representing the resulting constructed type. | |
MakePointerType | Returns a Type object that represents a pointer to the current type. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a String representing the name of the current Type. (Overrides Object.ToString().) |
Top
Fields
Name | Description | |
---|---|---|
Delimiter | Separates names in the namespace of the Type. This field is read-only. | |
EmptyTypes | Represents an empty array of type Type. This field is read-only. | |
FilterNameIgnoreCase | Represents the case-insensitive member filter used on names. This field is read-only. | |
Missing | Represents a missing value in the Type information. This field is read-only. |
Top
Remarks
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.
Type is an abstract base class. The system provides the internal (Friend in Visual Basic) derived class RuntimeType. In Silverlight-based applications, you cannot derive from Type.
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. |
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 and Module.GetType 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.GetModules method or through the Type.Module property.)
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 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).
Examples
The following code example shows a few representative features of Type. The C# typeof operator (GetType operator in Visual Basic) 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).
Note: |
---|
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.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Reflection
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
Dim t As Type = GetType(String)
Dim substr As MethodInfo = t.GetMethod("Substring", _
New Type() {GetType(Integer), GetType(Integer)})
Dim result As Object = _
substr.Invoke("Hello, World!", New Object() {7, 5})
outputBlock.Text += String.Format("{0} returned ""{1}"".", substr, result) & vbCrLf
End Sub
End Module
' This code example produces the following output:
'
'System.String Substring(Int32, Int32) returned "World".
using System;
using System.Reflection;
class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
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 });
outputBlock.Text += String.Format("{0} returned \"{1}\".", substr, result) + "\n";
}
}
/* This code example produces the following output:
System.String Substring(Int32, Int32) returned "World".
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
This type is thread safe.
See Also