System.Type Class

public abstract class Type : Object

Base Types

Object
  MemberInfo (excluded)
    Type

Assembly

mscorlib

Library

BCL

Summary

Provides information about a type.

Description

The Type class is abstract, as is the MemberInfo class and its subclasses FieldInfo, PropertyInfo, MethodBase, and EventInfo. ConstructorInfo and MethodInfo are subclasses of MethodBase. The runtime provides non-public implementations of these classes. [Note: For example, System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is typed as returning a MethodInfo object. The returned object is actually an instance of the non-public runtime type that implements MethodInfo .]

A conforming CLI program which is written to run on only the Kernel profile cannot subclass Type. [Note: This only applies to conforming programs not conforming implementations.]

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.

[Note: An instance of Type can represent any one of the following types:

The following table shows what members of a base class are returned by the methods that return members of types, such as System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) and System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]).

Member TypeStatic
ConstructorNo
FieldNo
EventNot applicable
MethodNo
Nested TypeNo
PropertyNot applicable

For reflection, properties and events are hide-by-name-and-signature. If a property has both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and the setter on the base class will not be accessible.

]

The description of System.Type.IsGenericType contains definitions for some important terms.

See Also

System Namespace

Members

Type Constructors

Type Constructor

Type Methods

Type.Equals Method
Type.GetArrayRank Method
Type.GetAttributeFlagsImpl Method
Type.GetConstructor(System.Reflection.BindingFlags, System.Reflection.Binder, System.Type[], System.Reflection.ParameterModifier[]) Method
Type.GetConstructor(System.Type[]) Method
Type.GetConstructors() Method
Type.GetConstructors(System.Reflection.BindingFlags) Method
Type.GetDefaultMembers Method
Type.GetElementType Method
Type.GetEvent(System.String) Method
Type.GetEvent(System.String, System.Reflection.BindingFlags) Method
Type.GetEvents() Method
Type.GetEvents(System.Reflection.BindingFlags) Method
Type.GetField(System.String, System.Reflection.BindingFlags) Method
Type.GetField(System.String) Method
Type.GetFields() Method
Type.GetFields(System.Reflection.BindingFlags) Method
Type.GetGenericArguments Method
Type.GetGenericParameterConstraints Method
Type.GetGenericTypeDefinition Method
Type.GetHashCode Method
Type.GetInterface(System.String, bool) Method
Type.GetInterface(System.String) Method
Type.GetInterfaces Method
Type.GetMember(System.String, System.Reflection.BindingFlags) Method
Type.GetMember(System.String) Method
Type.GetMembers(System.Reflection.BindingFlags) Method
Type.GetMembers() Method
Type.GetMethod(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Type[], System.Reflection.ParameterModifier[]) Method
Type.GetMethod(System.String, System.Reflection.BindingFlags) Method
Type.GetMethod(System.String) Method
Type.GetMethod(System.String, System.Type[]) Method
Type.GetMethod(System.String, System.Type[], System.Reflection.ParameterModifier[]) Method
Type.GetMethods(System.Reflection.BindingFlags) Method
Type.GetMethods() Method
Type.GetNestedType(System.String) Method
Type.GetNestedType(System.String, System.Reflection.BindingFlags) Method
Type.GetNestedTypes() Method
Type.GetNestedTypes(System.Reflection.BindingFlags) Method
Type.GetProperties(System.Reflection.BindingFlags) Method
Type.GetProperties() Method
Type.GetProperty(System.String, System.Type, System.Type[]) Method
Type.GetProperty(System.String, System.Type[]) Method
Type.GetProperty(System.String, System.Type) Method
Type.GetProperty(System.String) Method
Type.GetProperty(System.String, System.Reflection.BindingFlags) Method
Type.GetProperty(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Type, System.Type[], System.Reflection.ParameterModifier[]) Method
Type.GetPropertyImpl Method
Type.GetType(System.String, bool, bool) Method
Type.GetType(System.String, bool) Method
Type.GetType(System.String) Method
Type.GetTypeArray Method
Type.GetTypeFromHandle Method
Type.GetTypeHandle Method
Type.HasElementTypeImpl Method
Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Globalization.CultureInfo) Method
Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[]) Method
Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System.String[]) Method
Type.IsArrayImpl Method
Type.IsAssignableFrom Method
Type.IsByRefImpl Method
Type.IsCOMObjectImpl Method
Type.IsInstanceOfType Method
Type.IsPointerImpl Method
Type.IsPrimitiveImpl Method
Type.IsSubclassOf Method
Type.MakeArrayType() Method
Type.MakeArrayType(int) Method
Type.MakeByRefType Method
Type.MakeGenericType Method
Type.MakePointerType Method
Type.ToString Method

Type Fields

Type.Delimiter Field
Type.EmptyTypes Field
Type.Missing Field

Type Properties

Type.Assembly Property
Type.AssemblyQualifiedName Property
Type.Attributes Property
Type.BaseType Property
Type.ContainsGenericParameters Property
Type.DeclaringMethod Property
Type.DeclaringType Property
Type.DefaultBinder Property
Type.FullName Property
Type.GenericParameterAttributes Property
Type.GenericParameterPosition Property
Type.HasElementType Property
Type.IsAbstract Property
Type.IsAnsiClass Property
Type.IsArray Property
Type.IsAutoClass Property
Type.IsAutoLayout Property
Type.IsByRef Property
Type.IsClass Property
Type.IsEnum Property
Type.IsExplicitLayout Property
Type.IsGenericParameter Property
Type.IsGenericType Property
Type.IsGenericTypeDefinition Property
Type.IsImport Property
Type.IsInterface Property
Type.IsLayoutSequential Property
Type.IsMarshalByRef Property
Type.IsNestedAssembly Property
Type.IsNestedFamANDAssem Property
Type.IsNestedFamORAssem Property
Type.IsNestedFamily Property
Type.IsNestedPrivate Property
Type.IsNestedPublic Property
Type.IsNotPublic Property
Type.IsPointer Property
Type.IsPrimitive Property
Type.IsPublic Property
Type.IsSealed Property
Type.IsSpecialName Property
Type.IsUnicodeClass Property
Type.IsValueType Property
Type.Module Property
Type.Namespace Property
Type.ReflectedType Property
Type.TypeHandle Property
Type.TypeInitializer Property
Type.UnderlyingSystemType Property


Type Constructor

protected Type();

Summary

Constructs a new instance of the Type class.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.Equals Method

public bool Equals(Type o);

Summary

Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Type.

Parameters

o
The Type whose underlying system type is to be compared with the underlying system type of the current Type.

Return Value

true if the underlying system type of o is the same as the underlying system type of the current Type; otherwise, false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetArrayRank Method

public virtual int GetArrayRank();

Summary

Returns the number of dimensions in the current Type.

Return Value

A Int32 containing the number of dimensions in the current Type.

Exceptions

Exception TypeCondition
ArgumentExceptionThe current Type is not an array.

See Also

System.Type Class, System Namespace

Type.GetAttributeFlagsImpl Method

protected abstract TypeAttributes GetAttributeFlagsImpl();

Summary

When overridden in a derived type implements the System.Type.Attributes property and returns the attributes specified for the type represented by the current instance.

Return Value

A TypeAttributes value that signifies the attributes of the type represented by the current instance.

Description

[Behaviors: This property is read-only.

This method returns a TypeAttributes value that indicates the attributes set in the metadata of the type represented by the current instance.

]

[Usage: Use this property to determine the visibility, semantics, and layout format of the type represented by the current instance. Also use this property to determine if the type represented by the current instance has a special name.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetConstructor(System.Reflection.BindingFlags, System.Reflection.Binder, System.Type[], System.Reflection.ParameterModifier[]) Method

public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers);

Summary

Returns a constructor defined in the type represented by the current instance. The parameters of the constructor match the specified argument types and modifiers, under the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null.

binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the constructor to be returned.

modifiers
The only defined value for this parameter is null .

Return Value

A ConstructorInfo object that reflects the constructor that matches the specified criteria. If an exact match does not exist, binder will attempt to coerce the parameter types specified in types to select a match. If binder is unable to select a match, returns null . If the type represented by the current instance is contained in a loaded assembly, the constructor that matches the specified criteria is not public, and the caller does not have sufficient permissions, returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptiontypes is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The following BindingFlags are used to define which constructors to include in the search:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current instance represents an unassigned type parameter of a generic type or method, this method always returns null .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetConstructor(System.Type[]) Method

public ConstructorInfo GetConstructor(Type[] types);

Summary

Returns a public instance constructor defined in the type represented by the current instance. The parameters of the constructor match the specified argument types.

Parameters

types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the constructor to be returned. Specify System.Type.EmptyTypes to obtain a constructor that takes no parameters.

Return Value

A ConstructorInfo object representing the public instance constructor whose parameters match exactly the types in types , if found; otherwise, null . If the type represented by the current instance is contained in a loaded assembly, the constructor that matches the specified criteria is not public, and the caller does not have sufficient permissions, returns null .

If the current instance represents a generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current instance represents an unassigned type parameter of a generic type or method, this method always returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptiontypes is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

This version of System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null , types, null ).

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetConstructors() Method

public ConstructorInfo[] GetConstructors();

Summary

Returns an array of the public constructors defined in the type represented by the current instance.

Return Value

An array of ConstructorInfo objects that reflect the public constructors defined in the type represented by the current instance. If no public constructors are defined in the type represented by the current instance, or if the current instance represents an unassigned type parameter of a generic type or method, returns an empty array.

If the current instance represents a generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents a generic type parameter, the System.Type.GetConstructorsmethod returns an empty array.

Description

This version of System.Type.GetConstructors is equivalent to System.Type.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetConstructors(System.Reflection.BindingFlags) Method

public abstract ConstructorInfo[] GetConstructors(BindingFlags bindingAttr);

Summary

Returns an array of constructors defined in the type represented by the current instance, under the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of ConstructorInfo objects that reflect the constructors that are defined in the type represented by the current instance and match the constraints of bindingAttr. If System.Reflection.BindingFlags.NonPublic and System.Reflection.BindingFlags.Static are specified, this array includes the type initializer if it is defined. If no constructors meeting the constraints of bindingAttr are defined in the type represented by the current instance, or if the current instance represents an unassigned type parameter of a generic type or method, returns an empty array. If the type represented by the current instance is contained in a loaded assembly, the constructors that match the specified criteria are not public, and the caller does not have sufficient permission, returns null .

If the current instance represents a generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents a generic type parameter, the System.Type.GetConstructorsmethod returns an empty array.

Description

The following BindingFlags are used to define which constructors to include in the search:

[Note: For more information, see BindingFlags.]

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetDefaultMembers Method

public virtual MemberInfo[] GetDefaultMembers();

Summary

Returns an array of MemberInfo objects that reflect the default members defined in the type represented by the current instance.

Return Value

An array of MemberInfo objects reflecting the default members of the type represented by the current instance. If the type represented by the current instance does not have any default members, returns an empty array.

Description

If the current instance represents a generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the members of the class constraint, or the members of Object if there is no class constraint; the members of all interface constraints; and the members of any interfaces inherited from class or interface constraints.

[Behaviors: The members returned by this method have the DefaultMemberAttribute attribute.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetElementType Method

public abstract Type GetElementType();

Summary

Returns the element type of the current Type .

Return Value

A Type that represents the type used to create the current instance if the current instance represents an array, pointer, or an argument passed by reference. Otherwise, returns null if the current instance is not an array or a pointer, or is not passed by reference, or represents a generic type or a type parameter of a generic type or method.

Example

The following example demonstrates the System.Type.GetElementType method.

using System;
class TestType {
 public static void Main() {
 int[] array = {1,2,3};
 Type t = array.GetType();
 Type t2 = t.GetElementType();
 Console.WriteLine("{0} element type is {1}",array, t2.ToString());

 TestType newMe = new TestType();
 t = newMe.GetType();
 t2 = t.GetElementType();
 Console.WriteLine("{0} element type is {1}", newMe, t2==null? "null" : t2.ToString());
 }
}
The output is

System.Int32[] element type is System.Int32

TestType element type is null

See Also

System.Type Class, System Namespace

Type.GetEvent(System.String) Method

public EventInfo GetEvent(string name);

Summary

Returns a EventInfo object reflecting the public event that has the specified name and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the public event to be returned.

Return Value

A EventInfo object reflecting the public event that is named name and is defined in the type represented by the current instance, if found; otherwise, null .

If the current instance represents a generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

This version of System.Type.GetEvent(System.String) is equivalent to System.Type.GetEvent(System.String)( name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ).

The search for name is case-sensitive.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the events of the class constraint; the events of all interface constraints; and the events of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetEvent(System.String, System.Reflection.BindingFlags) Method

public abstract EventInfo GetEvent(string name, BindingFlags bindingAttr);

Summary

Returns a EventInfo object reflecting the event that has the specified name, is defined in the type represented by the current instance, and matches the specified binding constraints.

Parameters

name
A String containing the name of the event to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null.

Return Value

A EventInfo object reflecting the event that is named name, is defined in the type represented by the current instance, and matches the constraints of bindingAttr. If an event matching these criteria is not found, returns null . If the event is not public, the current instance represents a type from a loaded assembly, and the caller does not have sufficient permission, returns null .

If the current instance represents a generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which events to include in the search:

The following BindingFlags value can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the events of the class constraint; the events of all interface constraints; and the events of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetEvents() Method

public virtual EventInfo[] GetEvents();

Summary

Returns an array of EventInfo objects that reflect the public events defined in the type represented by the current instance.

Return Value

An array of EventInfo objects that reflect the public events defined in the type represented by the current instance. If no public events are defined in the type represented by the current instance, returns an empty array.

If the current instance represents a generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

Description

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the events of the class constraint; the events of all interface constraints; and the events of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

[Default: This version of System.Type.GetEvents is equivalent to System.Type.GetEvents(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance).]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetEvents(System.Reflection.BindingFlags) Method

public abstract EventInfo[] GetEvents(BindingFlags bindingAttr);

Summary

Returns an array of EventInfo objects that reflect the events that are defined in the type represented by the current instance and match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null.

Return Value

An array of EventInfo objects that reflect the events that are defined in the type represented by the current instance and match the constraints of bindingAttr . If no events match these constraints, returns an empty array. If the type reflected by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public events.

If the current instance represents a generic type, this method returns the EventInfo objects with the type parameters replaced by the appropriate type arguments.

Description

The following BindingFlags are used to define which events to include in the search:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the events of the class constraint; the events of all interface constraints; and the events of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetField(System.String, System.Reflection.BindingFlags) Method

public abstract FieldInfo GetField(string name, BindingFlags bindingAttr);

Summary

Returns a FieldInfo object reflecting the field that has the specified name, is defined in the type represented by the current instance, and matches the specified binding constraints.

Parameters

name
A String containing the name of the field to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

A FieldInfo object reflecting the field that is named name, is defined in the type represented by the current instance, and matches the constraints of bindingAttr. If a field matching these criteria cannot be found, returns null . If the field is not public, the current type is from a loaded assembly, and the caller does not have sufficient permission, returns null .

If the current instance represents a generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which fields to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the fields of the class constraint; the fields of all interface constraints; and the fields of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetField(System.String) Method

public FieldInfo GetField(string name);

Summary

Returns a FieldInfo object reflecting the field that has the specified name and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the field to be returned.

Return Value

A FieldInfo object reflecting the field that is named name and is defined in the type represented by the current instance, if found; otherwise, null . If the selected field is non-public, the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns null .

If the current instance represents a generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The search for name is case-sensitive.

This version of System.Type.GetField(System.String,System.Reflection.BindingFlags) is equivalent to System.Type.GetField(System.String,System.Reflection.BindingFlags)( name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance ).

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the fields of the class constraint; the fields of all interface constraints; and the fields of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetFields() Method

public FieldInfo[] GetFields();

Summary

Returns an array of FieldInfo objects that reflect the public fields defined in the type represented by the current instance.

Return Value

An array of FieldInfo objects that reflect the public fields defined in the type represented by the current instance. If no public fields are defined in the type represented by the current instance, returns an empty array.

If the current instance represents a generic type, this method returns the FieldInfo objects with the type parameters replaced by the appropriate type arguments.

Description

This version of System.Type.GetFields is equivalent to System.Type.GetFields( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ).

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the fields of the class constraint; the fields of all interface constraints; and the fields of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetFields(System.Reflection.BindingFlags) Method

public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);

Summary

Returns an array of FieldInfo objects that reflect the fields that are defined in the type represented by the current instance and match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of FieldInfo objects that reflect the fields that are defined in the type represented by the current instance and match the constraints of bindingAttr . If no fields match these constraints, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns only public fields.

If the current instance represents a generic type, this method returns the FieldInfo objects with the type parameters replaced by the appropriate type arguments.

Description

The following BindingFlags are used to define which fields to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the fields of the class constraint; the fields of all interface constraints; and the fields of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetGenericArguments Method

public virtual Type[] GetGenericArguments()

Summary

Returns an array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition.

Return Value

An array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition. Returns an empty array if the current type is not a generic type. The array elements are returned in the order in which they appear in the list of type arguments for the generic type.

Description

If the current type is a closed constructed type, the array returned by the System.Type.GetGenericArguments method contains the type arguments that are bound to the type parameters. If the current type is a generic type definition, the array contains the type parameters. If the current type is an open constructed type in which some of the type parameters are bound to specific types, the array contains both type arguments and type parameters.

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Example

For an example of using this method, see the example for System.Type.GenericParameterPosition.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetGenericParameterConstraints Method

public virtual Type[] GetGenericParameterConstraints()

Summary

Returns an array of Type objects that represent the type constraints on the current generic type parameter.

Return Value

An array of Type objects that represent the type constraints on the current generic type parameter.

Exceptions

Exception TypeCondition
InvalidOperationExceptionThe current Type object is not a generic type parameter. That is, the System.Type.IsGenericParameter property returns false .

Description

Each constraint on a generic type parameter is expressed as a Type object. The first element of the array is the class constraint, if any. If a type parameter has no class constraint and no interface constraints, an empty array of Type is returned for that type parameter. Use System.Reflection.GenericParameterAttributes to get the special constraints.

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetGenericTypeDefinition Method

public virtual Type GetGenericTypeDefinition()

Summary

Returns a Type object that represents a generic type from which the current type can be constructed.

Return Value

A Type object representing a generic type from which the current type can be constructed.

Exceptions

Exception TypeCondition
InvalidOperationExceptionThe current type is not a generic type. That is, System.Type.HasGenericArguments returns false .

Description

If two constructed types are created from the same generic type definition, the System.Type.GetGenericTypeDefinition method returns the same Type object for both types.

If you call System.Type.GetGenericTypeDefinition on a Type object that already represents a generic type definition, it returns the current Type.

[Note: An array type whose element type is a generic type is not itself generic. Use System.Type.IsGenericType to determine whether a type is generic before calling System.Type.GetGenericTypeDefinition.]

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Example

For an example of using this method, see the example for System.Type.MakeGenericType.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetHashCode Method

public override int GetHashCode();

Summary

Generates a hash code for the current instance.

Return Value

A Int32 containing the hash code for this instance.

Description

The algorithm used to generate the hash code is unspecified.

[Note: This method overrides System.Object.GetHashCode .]

See Also

System.Type Class, System Namespace

Type.GetInterface(System.String, bool) Method

public abstract Type GetInterface(string name, bool ignoreCase);

Summary

Returns the specified interface, specifying whether to do a case-sensitive search.

Parameters

name
A String containing the name of the interface to return.
ignoreCase
A Boolean where true indicates that the name search is to be done case-insensitively, and false performs a case-sensitive search.

Return Value

A Type object representing the interface with the specified name, implemented or inherited by the type represented by the instance, if found; otherwise, null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .
AmbiguousMatchExceptionThe current instance represents a type that implements the same generic interface with different type arguments.

Description

If the current instance represents a generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

[Note: Even with the introduction of generics, this method continues to return only non-generic members. To get the generic ones, one must call System.Type.GetInterfaces, and filter them out.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetInterface(System.String) Method

public Type GetInterface(string name);

Summary

Searches for the interface with the specified name.

Parameters

name
A String containing the name of the interface to get.

Return Value

A Type object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise, null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .
AmbiguousMatchExceptionThe current instance represents a type that implements the same generic interface with different type arguments.

Description

The search for name is case-sensitive.

If the current instance represents a generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

[Note: Even with the introduction of generics, this method continues to return only non-generic members. To get the generic ones, one must call System.Type.GetInterfaces, and filter them out.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetInterfaces Method

public abstract Type[] GetInterfaces();

Summary

Returns all interfaces implemented or inherited by the type represented by the current instance.

Return Value

An array of Type objects representing the interfaces implemented or inherited by the type represented by the current instance. If no interfaces are found, returns an empty Type array.

Description

If the current instance represents a generic type, this method returns the Type objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

[Note: Even with the introduction of generics, the overloads of System.Type.GetInterface continue to return only non-generic members. To get the generic ones, one must call System.Type.GetInterfaces, and filter them out.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMember(System.String, System.Reflection.BindingFlags) Method

public virtual MemberInfo[] GetMember(string name, BindingFlags bindingAttr);

Summary

Returns an array of MemberInfo objects that reflect the members defined in the type represented by the current instance that have the specified name and match the specified binding constraints.

Parameters

name
A String containing the name of the member to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of MemberInfo objects that reflect the members named name, are defined in the type represented by the current instance and match the constraints of bindingAttr . If no members match these constraints, returns an empty array. If the selected member is non-public, the type reflected by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the MemberInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the members of the class constraint, or the members of Object if there is no class constraint; the members of all interface constraints; and the members of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMember(System.String) Method

public MemberInfo[] GetMember(string name);

Summary

Returns an array of MemberInfo objects that reflect the public members that have the specified name and are defined in the type represented by the current instance.

Parameters

name
A String containing the name of the members to be returned.

Return Value

An array of MemberInfo objects that reflect the public members that are named name and are defined in the type represented by the current instance. If no public members with the specified name are defined in the type represented by the current instance, returns an empty array.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

This version of System.Type.GetMember(System.String) is equivalent to System.Type.GetMember(System.String)( name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public ).

The search for name is case-sensitive.

If the current instance represents a generic type, this method returns the MemberInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the members of the class constraint, or the members of Object if there is no class constraint; the members of all interface constraints; and the members of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMembers(System.Reflection.BindingFlags) Method

public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr);

Summary

Returns an array of MemberInfo objects that reflect the members that are defined in the type represented by the current instance and match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of MemberInfo objects that reflect the members defined in the type represented by the current instance that match the constraints of bindingAttr . If no members match these constraints, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have sufficient permission to reflect on non-public objects in loaded assemblies, returns only public members.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the members of the class constraint, or the members of Object if there is no class constraint; the members of all interface constraints; and the members of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMembers() Method

public MemberInfo[] GetMembers();

Summary

Returns an array of MemberInfo objects that reflect the public members defined in the type represented by the current instance.

Return Value

An array of MemberInfo objects that reflect the public members defined in the type represented by the current instance. If no public members are defined in the type represented by the current instance, returns an empty array.

Description

This version of System.Type.GetMembers is equivalent to System.Type.GetMembers(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance).

If the current instance represents a generic type, this method returns the MemberInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the members of the class constraint, or the members of Object if there is no class constraint; the members of all interface constraints; and the members of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethod(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Type[], System.Reflection.ParameterModifier[]) Method

public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers);

Summary

Returns a MethodInfo object that reflects the method that matches the specified criteria and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the method to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned.

modifiers
The only defined value for this parameter is null .

Return Value

A MethodInfo object that reflects the method defined in the type represented by the current instance that matches the specified criteria. If no method matching the specified criteria is found, returns null . If the selected method is non-public, the type reflected by the current instance is from a loaded assembly, and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one method matching the specified criteria was found.
ArgumentNullExceptionname or types is null .

-or-

At least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethod(System.String, System.Reflection.BindingFlags) Method

public MethodInfo GetMethod(string name, BindingFlags bindingAttr);

Summary

Returns a MethodInfo object that reflects the method that has the specified name and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the method to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

A MethodInfo object that reflects the method that is defined in the type represented by the current instance and matches the specified criteria, if found; otherwise, null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one method matching the specified criteria was found.
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr , null , null , null ).

If the current instance represents a generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethod(System.String) Method

public MethodInfo GetMethod(string name);

Summary

Returns a MethodInfo object that reflects the public method that has the specified name and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the public method to be returned.

Return Value

A MethodInfo object reflecting the public method that is defined in the type represented by the current instance and has the specified name, if found; otherwise, null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one method matching the specified criteria was found.
ArgumentNullExceptionname is null .

Description

The search for name is case-sensitive.

This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance, null , null , null ).

If the current instance represents a generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethod(System.String, System.Type[]) Method

public MethodInfo GetMethod(string name, Type[] types);

Summary

Returns a MethodInfo object that reflects the public method defined in the type represented by the current instance that has the specified name and parameter information.

Parameters

name
A String containing the name of the public method to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned.

Return Value

A MethodInfo object reflecting the public method defined in the type represented by the current instance that matches the specified criteria. If no public method matching the specified criteria is found, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one method matching the specified criteria was found.
ArgumentNullExceptionname or types is null .

-or-

At least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The search for name is case-sensitive.

This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance, null , types, null ).

If the current instance represents a generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethod(System.String, System.Type[], System.Reflection.ParameterModifier[]) Method

public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers);

Summary

Returns a MethodInfo object that reflects the public method that has the specified name and is defined in the type represented by the current instance.

Parameters

name
A String containing the name of the public method to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the method to be returned.

modifiers
The only defined value for this parameter is null .

Return Value

A MethodInfo object reflecting the public method that is defined in the type represented by the current instance and matches the specified criteria, if found; otherwise, null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one method matching the specified criteria was found.
ArgumentNullExceptionname or types is null .

-or-

At least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The default binder does not process modifier .

The search for name is case-sensitive.

This version of System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) ( name, System.Reflection.BindingFlags.Public |System.Reflection.BindingFlags.Static |System.Reflection.BindingFlags.Instance , null , types , modifiers).

If the current instance represents a generic type, this method returns the MethodInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethods(System.Reflection.BindingFlags) Method

public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);

Summary

Returns an array of MethodInfo objects that reflect the methods defined in the type represented by the current instance that match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of MethodInfo objects reflecting the methods defined in the type represented by the current instance that match the constraints of bindingAttr . If no such methods found, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public methods.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetMethods() Method

public MethodInfo[] GetMethods();

Summary

Returns the public methods defined in the type represented by the current instance.

Return Value

An array of MethodInfo objects reflecting the public methods defined in the type represented by the current instance. If no methods are found, returns an empty array.

Description

This version of System.Type.GetMethods is equivalent to System.Type.GetMethods( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ).

If the current instance represents a generic type, this method returns the MethodInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the methods of the class constraint, or the methods of Object if there is no class constraint; the methods of all interface constraints; and the methods of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetNestedType(System.String) Method

public Type GetNestedType(string name);

Summary

Returns the public nested type defined in the type represented by the current instance

Parameters

name
A String containing the name of the public nested type to return. Specify the unqualified name of the nested type. [Note: For example, for a type B nested within A, if typeA represents the type object for A, the correct invocation is typeA.GetNestedType("B"). ]

Return Value

A Type object representing the public nested type with the specified name, if found; otherwise, null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The search for name is case-sensitive.

Use the simple name of the nested class for name; do not qualify it with the name of the outer class. CLS rules require a naming pattern for nested types; see Partition I.

If the current instance represents an unassigned type parameter of a generic type or method definition, this method does not search the nested types of the class constraint.

[Note: The name parameter cannot include type arguments. For example, passing "MyGenericNestedType<int>" to this method searches for a nested type with the text name "MyGenericNestedType<int>", rather than for a nested type named MyGenericNestedType that has one generic argument of type int .]

[Note: If the nested type is generic, what is returned is always a generic type definition.]

For information on constructing nested generic types from their generic type definitions, see the System.Type.MakeGenericType(System.Type[]) method.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetNestedType(System.String, System.Reflection.BindingFlags) Method

public abstract Type GetNestedType(string name, BindingFlags bindingAttr);

Summary

Returns a nested types defined in the type represented by the current instance that match the specified binding constraints.

Parameters

name
A String containing the name of the nested type to return. Specify the unqualified name of the nested type. [Note: For example, for a type B nested within A, if typeA represents the type object for A, the correct invocation is typeA.GetNestedType("B").]

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

A Type object representing the nested type that matches the specified criteria, if found; otherwise, null . If the selected nested type is non-public, the current instance represents a type contained in a loaded assembly and the caller does not have sufficient permissions, returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the nested types of the class constraint.

[Note: The name parameter cannot include type arguments.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetNestedTypes() Method

public Type[] GetNestedTypes();

Summary

Returns all the public types nested within the current Type.

Return Value

An array of Type objects representing all public types nested within the type represented by the current instance, if any. Otherwise, returns an empty Type array.

Description

This version of System.Type.GetNestedTypes is equivalent to System.Type.GetNestedTypes(System.Reflection.BindingFlags.Public).

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the nested types of the class constraint.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetNestedTypes(System.Reflection.BindingFlags) Method

public abstract Type[] GetNestedTypes(BindingFlags bindingAttr);

Summary

Returns an array containing the nested types defined in the type represented by the current instance that match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of Type objects representing all types nested within the type represented by the current instance that match the specified binding constraints, if any. Otherwise, returns an empty Type array. If the type reflected by the current instance is contained in a loaded assembly, the type that matches the specified criteria is not public, and the caller does not have sufficient permission, returns only public types.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the nested types of the class constraint.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperties(System.Reflection.BindingFlags) Method

public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);

Summary

Returns an array of PropertyInfo objects that reflect the properties defined for the type represented by the current instance that match the specified binding constraints.

Parameters

bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

An array of PropertyInfo objects that reflect the properties defined in the type represented by the current instance and match the constraints of bindingAttr . If no matching properties are found, returns an empty array. If the type represented by the current instance is from a loaded assembly and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns only public properties.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

If the current instance represents a generic type, this method returns the PropertyInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

[Behaviors: A property is considered by reflection to be public if it has at least one accessor that is public . Otherwise, the property is not public .]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperties() Method

public PropertyInfo[] GetProperties();

Summary

Returns an array of PropertyInfo objects that reflect the public properties defined in the type represented by the current instance.

Return Value

An array of PropertyInfo objects that reflect the public properties defined in the type represented by the current instance. If no public properties are found, returns an empty array.

Description

This version of System.Type.GetProperties(System.Reflection.BindingFlags) is equivalent to System.Type.GetProperties(System.Reflection.BindingFlags)( System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public ).

A property is considered by reflection to be public if it has at least one accessor that is public . Otherwise, the property is considered to be not public .

If the current instance represents a generic type, this method returns the PropertyInfo objects with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String, System.Type, System.Type[]) Method

public PropertyInfo GetProperty(string name, Type returnType, Type[] types);

Summary

Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.

Parameters

name
A String containing the name of the public property to be returned.
returnType
A Type object that represents the type of the public property to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes for a property that is not indexed.

Return Value

A PropertyInfo object reflecting the public property defined in the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname or types is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null , returnTypes, types, null ).

The search for name is case-sensitive.

Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String, System.Type[]) Method

public PropertyInfo GetProperty(string name, Type[] types);

Summary

Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.

Parameters

name
A String containing the name of the public property to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed.

Return Value

A PropertyInfo object reflecting the public property defined on the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname or types is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null , null , types, null ).

The search for name is case-sensitive.

Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String, System.Type) Method

public PropertyInfo GetProperty(string name, Type returnType);

Summary

Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.

Parameters

name
A String containing the name of the property to be returned.
returnType
A Type object that represents the type of the property to be returned.

Return Value

A PropertyInfo object reflecting the public property defined on the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname is null .

Description

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null , returnType, null , null ).

The search for name is case-sensitive.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String) Method

public PropertyInfo GetProperty(string name);

Summary

Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that has the specified name.

Parameters

name
A String containing the name of the property to be returned.

Return Value

A PropertyInfo object reflecting the public property defined on the type represented by the current instance that has the specified name. If no matching property is found, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname is null .

Description

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, null , null , null , null ).

The search for name is case-sensitive.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String, System.Reflection.BindingFlags) Method

public PropertyInfo GetProperty(string name, BindingFlags bindingAttr);

Summary

Returns a PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria.

Parameters

name
A String containing the name of the property to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

Return Value

A PropertyInfo object reflecting the property defined in the type represented by the current instance that matches the specified criteria. If no matching property is found, returns null . If the type reflected by the current instance is contained in a loaded assembly, the property that matches the specified criteria is not public, and the caller does not have sufficient permission, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname is null .

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr, null , null , null , null ).

The search for name is case-sensitive.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetProperty(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Type, System.Type[], System.Reflection.ParameterModifier[]) Method

public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);

Summary

Returns a PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria .

Parameters

name
A String containing the name of the property to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

returnType
A Type object that represents the type of the property to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed.

modifiers
The only defined value for this parameter is null .

Return Value

A PropertyInfo object reflecting the property that is defined in the type represented by the current instance and matches the specified criteria. If no matching property is found, returns null . If the type reflected by the current instance is contained in a loaded assembly, the property that matches the specified criteria is not public, and the caller does not have sufficient permission, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname or types is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

This version of System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) is equivalent to System.Type.GetPropertyImpl(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[])(name, bindingAttr, binder, returnType, types, modifiers).

Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly.

If the current instance represents a generic type, this method returns the System.Reflection.PropertyInfo with the type parameters replaced by the appropriate type arguments.

If the current instance represents an unassigned type parameter of a generic type or method, this method searches the properties of the class constraint; the properties of all interface constraints; and the properties of any interfaces inherited from class or interface constraints.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetPropertyImpl Method

protected abstract PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);

Summary

When overridden in a derived class implements the System.Type.GetProperty(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Type,System.Type[],System.Reflection.ParameterModifier[]) method and returns a PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria.

Parameters

name
A String containing the name of the property to be returned.
bindingAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null .

binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

returnType
A Type object that represents the type of the property to be returned.
types
An array of Type objects. The elements in the array are of the same number, in the same order, and represent the same types as the parameters for the indexer to be returned. Specify System.Type.EmptyTypes to obtain a property that is not indexed.

modifiers
The only defined value for this parameter is null .

Return Value

A PropertyInfo object representing the property that matches the specified search criteria, if found; otherwise, null . If the type reflected by the current instance is from a loaded assembly, the matching property is not public, and the caller does not have permission to reflect on non-public objects in loaded assemblies, returns null .

Exceptions

Exception TypeCondition
AmbiguousMatchExceptionMore than one property matching the specified criteria was found.
ArgumentNullExceptionname or types is null , or at least one of the elements in types is null .

ArgumentExceptiontypes has more than one dimension.

Description

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

[Behaviors: Different programming languages use different syntax to specify indexed properties. Internally, this property is referred to by the name "Item" in the metadata. Therefore, any attempt to retrieve an indexed property using reflection is required to specify this internal name in order for the PropertyInfo to be returned correctly. ]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetType(System.String, bool, bool) Method

public static Type GetType(string typeName, bool throwOnError, bool ignoreCase);

Summary

Returns the Type with the specified name, optionally performing a case-insensitive search and optionally throwing an exception if an error occurs while loading the Type.

Parameters

typeName
A String containing the name of the Type to return.
throwOnError
A Boolean. Specify true to throw a TypeLoadException if an error occurs while loading the Type. Specify false to ignore errors while loading the Type.

ignoreCase
A Boolean. Specify true to perform a case-insensitive search for typeName. Specify false to perform a case-sensitive search for typeName .

Return Value

The Type with the specified name, if found; otherwise, null . If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptiontypeName is null .
TargetInvocationExceptionA type initializer was invoked and threw an exception.
TypeLoadExceptionthrowOnError is true and an error was encountered while loading the selected Type.

Description

typeName can be a simple type name, a fully qualified name, or a complex name that includes an assembly name. [Note: System.Type.AssemblyQualifiedName returns a fully qualified type name including nested types, the assembly name, and generic type arguments. ]

If typeName includes only the name of the Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly.

[Note:

The following table shows calls to GetType for various types. (Some long strings have been wrapped to fit in the right column.)

To Get this TypeUse this String
An unmanaged pointer to MyType Type.GetType("MyType*")
An unmanaged pointer to a pointer to MyType Type.GetType("MyType**")
A managed pointer or reference to MyType Type.GetType("MyType&")Note that unlike pointers, references are limited to one level.
A parent class and a nested classType.GetType("MyParentClass+MyNestedClass")
A one-dimensional array with a lower bound of 0Type.GetType("MyArray[]")
A one-dimensional array with an unknown lower boundType.GetType("MyArray[*]")
An n-dimensional arrayA comma (,) inside the brackets a total of n-1 times. For example, System.Object[,,] represents a three-dimensional Object array.
A two-dimensional array's arrayType.GetType("MyArray[][]")
A rectangular two-dimensional array with unknown lower boundsType.GetType("MyArray[*,*]") or Type.GetType("MyArray[,]")
A generic type with one type argumentType.GetType("MyGenericType[MyType]")
A generic type with two type argumentsType.GetType("MyGenericType[MyType,AnotherType]")
A generic type with two assembly-qualified type argumentsType.GetType("MyGenericType[[MyType,MyAssembly], [AnotherType,AnotherAssembly]]")
An assembly-qualified generic type with an assembly-qualified type argumentType.GetType("MyGenericType[[MyType,MyAssembly]] ,MyGenericTypeAssembly")
A generic type whose type argument is a generic type with two type argumentsType.GetType("MyGenericType[AnotherGenericType [MyType,AnotherType]]")
]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetType(System.String, bool) Method

public static Type GetType(string typeName, bool throwOnError);

Summary

Returns the Type with the specified name, optionally throwing an exception if an error occurs while loading the Type .

Parameters

typeName
A String containing the case-sensitive name of the Type to return.
throwOnError
A Boolean. Specify true to throw a TypeLoadException if an error occurs while loading the Type. Specify false to ignore errors while loading the Type.

Return Value

The Type with the specified name, if found; otherwise, null . If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptiontypeName is null .
TargetInvocationExceptionA type initializer was invoked and threw an exception.
TypeLoadExceptionthrowOnError is true and an error was encountered while loading the Type.

Description

This method is equivalent to System.Type.GetType(System.String,System.Boolean,System.Boolean)(name, throwOnError, false ).

typeName can be a simple type name, a fully qualified name, or a complex name that includes an assembly name specification. If typeName includes only the name of the Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly.

[Note: System.Type.AssemblyQualifiedName can return a fully qualified type name including nested types, the assembly name, and generic type arguments. For complete details, see System.Type.GetType(System.String,System.Boolean,System.Boolean)(String, Boolean, Boolean). ]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetType(System.String) Method

public static Type GetType(string typeName);

Summary

Returns the Type with the specified name.

Parameters

typeName
A String containing the case-sensitive name of the Type to return.

Return Value

The Type with the specified name, if found; otherwise, null . If the requested type is non-public and the caller does not have permission to reflect non-public objects outside the current assembly, this method returns null .

Exceptions

Exception TypeCondition
ArgumentNullExceptiontypeName is null .
TargetInvocationExceptionA type initializer was invoked and threw an exception.

Description

This method is equivalent to System.Type.GetType(System.String,System.Boolean,System.Boolean)(name, false , false ).

typeName can be a simple type name, a type name that includes a namespace, or a complex name that includes an assembly name specification. If typeName includes only the name of the Type, this method searches in the calling object's assembly, then in the mscorlib.dll assembly. If typeName is fully qualified with the partial or complete assembly name, this method searches in the specified assembly.

[Note: System.Type.AssemblyQualifiedName can return a fully qualified type name including nested types, the assembly name, and generic type arguments. For complete details, see System.Type.GetType(System.String,System.Boolean,System.Boolean)(String, Boolean, Boolean).]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetTypeArray Method

public static Type[] GetTypeArray(object[] args);

Summary

Returns the types of the objects in the specified array.

Parameters

args
An array of objects whose types are to be returned.

Return Value

An array of Type objects representing the types of the corresponding elements in args. If a requested type is not public and the caller does not have permission to reflect non-public objects outside the current assembly, the corresponding element in the array returned by this method will be null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionargs is null .
TargetInvocationExceptionThe type initializers were invoked and at least one threw an exception.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GetTypeFromHandle Method

public static Type GetTypeFromHandle(RuntimeTypeHandle handle);

Summary

Gets the Type referenced by the specified type handle.

Parameters

handle
The RuntimeTypeHandle object that refers to the desired Type.

Return Value

The Type referenced by the specified RuntimeTypeHandle.

Exceptions

Exception TypeCondition
ArgumentNullExceptionhandle is null .
SecurityExceptionThe requested type is non-public and outside the current assembly, and the caller does not have the required permission.
TargetInvocationExceptionA type initializer was invoked and threw an exception.

Description

The handles are valid only in the application domain in which they were obtained.

Library

RuntimeInfrastructure

See Also

System.Type Class, System Namespace

Type.GetTypeHandle Method

public static RuntimeTypeHandle GetTypeHandle(object o);

Summary

Returns the handle for the Type of the specified object.

Parameters

o
The object for which to get the type handle.

Return Value

The RuntimeTypeHandle for the Type of the specified Object.

Description

The handle is valid only in the application domain in which it was obtained.

Library

RuntimeInfrastructure

See Also

System.Type Class, System Namespace

Type.HasElementTypeImpl Method

protected abstract bool HasElementTypeImpl();

Summary

When overridden in a derived class, implements the System.Type.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.

Return Value

true if the Type is an array, a pointer, or is passed by reference; otherwise, false .

Description

[Note: For example, System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32[]").HasElementTypeImpl returns true , but System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32").HasElementTypeImpl returns false . System.Type.HasElementTypeImpl also returns true for "System.Int32*" and "System.Int32&".]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Globalization.CultureInfo) Method

public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, CultureInfo culture);

Summary

Invokes the specified member, using the specified binding constraints and matching the specified argument list and culture.

Parameters

name
A String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. [Note: For more information on default members, see DefaultMemberAttribute .]

invokeAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default.

binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

target
A Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored.
args
An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound. Specify an empty array or null for a member that has no parameters.
culture
The only defined value for this parameter is null .

Return Value

A Object containing the return value of the invoked member. If the invoked member does not have a return value, returns a Object containing Void .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

ArgumentExceptionargs has more than one dimension.

-or-

invokeAttr is not a valid BindingFlags value.

-or-

The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr.

-or-

The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr.

-or-

The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr.

-or-

The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr.

-or-

invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.InvokeMethodand at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element.

MissingFieldExceptionA field or property matching the specified criteria was not found.
MissingMethodExceptionA method matching the specified criteria was not found.

-or-

The current instance object represents a type that contains open type parameters (that is,

System.Type.ContainsGenericParameters returns

true ).

MethodAccessExceptionThe requested member is non-public and the caller does not have the required permission.
TargetExceptionThe member matching the specified criteria cannot be invoked on target.
TargetInvocationExceptionThe member matching the specified criteria threw an exception.
AmbiguousMatchExceptionMore than one member matches the specified criteria.

Description

This version of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) is equivalent to System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( name, invokeAttr, binder, target, args, null , culture, null ).

Example

For an example that demonstrates System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) , see System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).

Library

Reflection

See Also

System.Type Class, System Namespace

Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[]) Method

public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);

Summary

Invokes the specified member, using the specified binding constraints and matching the specified argument list.

Parameters

name
A String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. [Note: For more information on default members, see DefaultMemberAttribute .]

invokeAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default.
binder
A Binder object that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to use the System.Type.DefaultBinder .

target
A Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored.
args
An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound. Specify an empty array or null for a member that has no parameters.

Return Value

A Object containing the return value of the invoked member. If the invoked member does not have a return value, returns a Object containing Void .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

ArgumentExceptionargs has more than one dimension.

-or-

invokeAttr is not a valid BindingFlags value.

-or-

The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr.

-or-

The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr.

-or-

The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr.

-or-

The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr.

-or-

invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.InvokeMethod and at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element.

MissingFieldExceptionA field or property matching the specified criteria was not found.
MissingMethodExceptionA method matching the specified criteria cannot be found.

-or-

The current instance object represents a type that contains open type parameters (that is,

System.Type.ContainsGenericParameters returns

true ).

MethodAccessExceptionThe requested member is non-public and the caller does not have the required permission.
TargetExceptionThe member matching the specified criteria cannot be invoked on target.
TargetInvocationExceptionThe member matching the specified criteria threw an exception.
AmbiguousMatchExceptionMore than one member matches the specified criteria.

Description

This version of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) is equivalent to System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])(name, invokeAttr, binder, target, args, null , null , null ).

[Note: For a demonstration of the use of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]), see the example for System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[])( String, BindingFlags, Binder, Object, Object[], ParameterModifier[], CultureInfo, String[]).]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.InvokeMember(System.String, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object, System.Object[], System.Reflection.ParameterModifier[], System.Globalization.CultureInfo, System.String[]) Method

public abstract object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);

Summary

Invokes or accesses a member defined on the type represented by the current instance that matches the specified binding criteria.

Parameters

name
A String containing the name of the constructor or method to invoke, or property or field to access. If the type represented by the current instance has a default member, specify System.String.Empty to invoke that member. [Note: For more information on default members, see DefaultMemberAttribute .]

invokeAttr
A bitwise combination of BindingFlags values that control the binding process. If zero is specified, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance is used by default.

binder
A Binder that defines a set of properties, and enables the binding, coercion of argument types, and invocation of members using reflection. Specify null to useSystem.Type.DefaultBinder.
target
A Object on which to invoke the member that matches the other specified criteria. If the matching member is static , this parameter is ignored.
args
An array of objects containing the arguments to pass to the member to be invoked. The elements of this array are of the same number and in the same order by assignment-compatible type as specified by the contract of the member to be bound if and only if nameParameters is null . If namedParameters is not null , the order of the elements in args corresponds to the order of the parameters specified in namedParameters. Specify an empty array or null for a member that takes no parameters.
modifiers
The only defined value for this parameter is null .

culture
The only defined value for this parameter is null .

namedParameters
An array of String objects containing the names of the parameters to which the values in args are passed. These names are processed in a case-sensitive manner and have a one-to-one correspondence with the elements of args. Specify an empty array or null for a member that takes no parameters. Specify null to have this parameter ignored.

Return Value

A Object containing the return value of the invoked or accessed member. If the member does not have a return value, returns a Object containing Void .

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null .

ArgumentExceptionargs has more than one dimension.

-or-

invokeAttr is not a valid BindingFlags value.

-or-

The member to be invoked is a constructor and System.Reflection.BindingFlags.CreateInstance is not specified in invokeAttr.

-or-

The member to be invoked is a method that is not a type initializer or instance constructor, and System.Reflection.BindingFlags.InvokeMethod is not specified in invokeAttr.

-or-

The member to be accessed is a field, and neither System.Reflection.BindingFlags.GetField nor System.Reflection.BindingFlags.SetField is specified in invokeAttr.

-or-

The member to be accessed is a property, and neither System.Reflection.BindingFlags.GetProperty nor System.Reflection.BindingFlags.SetProperty is specified in invokeAttr.

-or-

invokeAttr contains System.Reflection.BindingFlags.CreateInstance and at least one of System.Reflection.BindingFlags.InvokeMethod, System.Reflection.BindingFlags.GetField, System.Reflection.BindingFlags.SetField, System.Reflection.BindingFlags.GetProperty, or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetField and System.Reflection.BindingFlags.SetField.

-or-

invokeAttr contains both System.Reflection.BindingFlags.GetProperty and System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.InvokeMethod and at least one of System.Reflection.BindingFlags.SetField or System.Reflection.BindingFlags.SetProperty.

-or-

invokeAttr contains System.Reflection.BindingFlags.SetField and args has more than one element.

-or-

namedParameters.Length > args.Length .

-or-

At least one element in namedParameters is null .

-or-

At least one element in args is not assignment-compatible with the corresponding parameter in namedParameters.

MissingFieldExceptionA field or property matching the specified criteria was not found.
MissingMethodExceptionA method matching the specified criteria cannot be found.

-or-

The current instance object represents a type that contains open type parameters (that is,

System.Type.ContainsGenericParameters returns

true ).

MethodAccessExceptionThe requested member is non-public and the caller does not have the required permission.
TargetExceptionThe member matching the specified criteria cannot be invoked on target.
TargetInvocationExceptionThe member matching the specified criteria threw an exception.
AmbiguousMatchExceptionMore than one member matches the specified criteria.

Description

System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) calls a constructor or a method , gets or sets a property , gets or sets a field, or gets or sets an element of an array.

The binder finds all of the matching members. These members are found based upon the type of binding specified by invokeAttr . The System.Reflection.Binder.BindToMethod(System.Reflection.BindingFlags,System.Reflection.MethodBase[],System.Object[]@,System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[],System.Object@) is responsible for selecting the method to be invoked. The default binder selects the most specific match. The set of members is then filtered by name, number of arguments, and a set of search modifiers defined in the binder. After the member is selected, it is invoked or accessed. Accessibility is checked at that point. Access restrictions are ignored for fully trusted code; that is, private constructors, methods, fields, and properties can be accessed and invoked via reflection whenever the code is fully trusted.

The following BindingFlags are used to define which members to include in the search:

The following BindingFlags values can be used to change how the search works:

[Note: For more information, see BindingFlags.]

[Behaviors: Each parameter in the namedParameters array is assigned the value in the corresponding element in the args array. If the length of args is greater than the length of namedParameters, the remaining argument values are passed in order.

A member will be found only if the number of parameters in the member declaration equals the number of arguments in the args array (unless default arguments are defined on the member). Also, The type of each argument is required to be convertible by the binder to the type of the parameter.

It is required that the caller specify values for invokeAttr as follows:

ActionBindingFlags
Invoke a constructor. System.Reflection.BindingFlags.CreateInstance. This flag is not valid with the other flags in this table. If this flag is specified, name is ignored.
Invoke a method. System.Reflection.BindingFlags.InvokeMethod. This flag if not valid with System.Reflection.BindingFlags.CreateInstance, System.Reflection.BindingFlags.SetField, or System.Reflection.BindingFlags.SetProperty.
Define a field value. System.Reflection.BindingFlags.SetField. This flag is not valid with System.Reflection.BindingFlags.CreateInstance, System.Reflection.BindingFlags.InvokeMethod, or System.Reflection.BindingFlags.GetField.
Return a field value. System.Reflection.BindingFlags.GetField. This flag is not valid with System.Reflection.BindingFlags.CreateInstance, System.Reflection.BindingFlags.InvokeMethod, or System.Reflection.BindingFlags.SetField.
Set a property. System.Reflection.BindingFlags.SetProperty. This flag is not valid with System.Reflection.BindingFlags.CreateInstance, System.Reflection.BindingFlags.InvokeMethod, or System.Reflection.BindingFlags.GetProperty.
Get a property.System.Reflection.BindingFlags.GetProperty. This flag is not valid with System.Reflection.BindingFlags.CreateInstance, System.Reflection.BindingFlags.InvokeMethod, or System.Reflection.BindingFlags.SetProperty.

[Note: For more information, see BindingFlags .]

]

[Usage: System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) can be used to invoke methods with parameters that have default values. To bind to these methods, System.Reflection.BindingFlags.OptionalParamBinding must be specified. For a parameter that has a default value, the caller can supply a value or supply System.Type.Missing to use the default value.

System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) can be used to set a field to a particular value by specifying System.Reflection.BindingFlags.SetField . For example, to set a public instance field named F on class C, where F is a string, the value is set using the following statement:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{ "strings new value"}, null, null, null);

A string array F can be initialized as follows:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{new String[]{"a","z","c","d"}, null, null, null);

Use System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) to set the value of an element in an array by specifying the index of the value and the new value for the element as follows:

typeof(C).InvokeMember("F", BindingFlags.SetField, null, C, new Object{1, "b"}, null, null, null);

The preceding statement changes "z" in array F to "b".

]

Example

The following example demonstrates the use of System.Type.InvokeMember(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Object,System.Object[],System.Reflection.ParameterModifier[],System.Globalization.CultureInfo,System.String[]) to construct a String, obtain its System.String.Length property, invoke System.String.Insert(System.Int32,System.String) on it, and then set its value using the System.String.Empty field.

using System;
using System.Reflection;

class InvokeMemberExample
{
   static void Main(string[] args)
   {
      // Create the parameter arrays that will
      // be passed to InvokeMember.
      char[] cAry = 
      new char[] {'A',' ','s','t','r','i','n','g'};
      object[] oAry = new object[] {cAry, 0, cAry.Length};

      Type t = typeof(string);

      // Invoke the constructor of a string.
      string str =
         (string)t.InvokeMember(null, BindingFlags.Instance |
         BindingFlags.Public | BindingFlags.CreateInstance, null,
         null, oAry, null, null, null);
      Console.WriteLine("The string is \"{0}\".", str);

      // Access a property of the string.
      int i =
         (int) t.InvokeMember("Length", BindingFlags.Instance |
         BindingFlags.Public | BindingFlags.GetProperty, null, 
         str, null, null, null, null);
      Console.WriteLine("The length of the string is {0}.", i);

      // Invoke a method on the string.
      string newStr = "new ";
      object[] oAry2 = new Object[] {2, newStr};
      str = (string) t.InvokeMember("Insert", BindingFlags.Instance |
         BindingFlags.Public | BindingFlags.InvokeMethod, null, str, 
         oAry2, null, null, null);
      Console.WriteLine("The modified string is \"{0}\".", str);

      // Access a field of the string.
      str = (string) t.InvokeMember("Empty", BindingFlags.Static | 
         BindingFlags.Public | BindingFlags.GetField, null, str, 
         null);
      Console.WriteLine("The empty string is \"{0}\".", str);
  
   }
}
The output is

The string is "A string".

The length of the string is 8.

The modified string is "A new string"

The empty string is "".

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsArrayImpl Method

protected abstract bool IsArrayImpl();

Summary

When overridden in a derived class implements the System.Type.IsArray property returning a Boolean value that indicates whether the type represented by the current instance is an array.

Return Value

true if the Type is an array; otherwise, false .

Description

An instance of the Array class is required to return false because it is an object, not an array.[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsAssignableFrom Method

public virtual bool IsAssignableFrom(Type c);

Summary

Determines whether an instance of the current Type can be assigned from an instance of the specified Type .

Parameters

c
The Type to compare with the current Type .

Return Value

false if c is a null reference.

true if one or more of the following statements are true; otherwise false .

Description

[Note: A generic type definition is not assignable from a closed constructed type.

]

Example

The following example demonstrates the System.Type.IsAssignableFrom(System.Type) method using arrays.

using System;
class ArrayTypeTest {
 public static void Main() {
 int i = 1;
 int [] array10 = new int [10];
 int [] array2 = new int[2];
 int [,]array22 = new int[2,2];
 int [,]array24 = new int[2,4];
 int [,,]array333 = new int[3,3,3];
 Type array10Type = array10.GetType();
 Type array2Type = array2.GetType();
 Type array22Type = array22.GetType();
 Type array24Type = array24.GetType();
 Type array333Type = array333.GetType();

 // If X and Y are not both arrays, then false
 Console.WriteLine("int[2] is assignable from int? {0} ", array2Type.IsAssignableFrom(i.GetType()));
 // If X and Y have same type and rank, then true.
 Console.WriteLine("int[2] is assignable from int[10]? {0} ",  array2Type.IsAssignableFrom(array10Type));
 Console.WriteLine("int[2,2] is assignable from int[2,4]? {0}",  array22Type.IsAssignableFrom(array24Type));
 Console.WriteLine("int[2,4] is assignable from int[2,2]? {0}",  array24Type.IsAssignableFrom(array22Type));
 Console.WriteLine("");
 // If X and Y do not have the same rank, then false.
 Console.WriteLine("int[2,2] is assignable from int[10]? {0}",  array22Type.IsAssignableFrom(array10Type));
 Console.WriteLine("int[2,2] is assignable from int[3,3,3]? {0}",  array22Type.IsAssignableFrom(array333Type));
 Console.WriteLine("int[3,3,3] is assignable from int[2,2]? {0}",  array333Type.IsAssignableFrom(array22Type));
 }
}
The output is

int[2] is assignable from int? False

int[2] is assignable from int[10]? True

int[2,2] is assignable from int[2,4]? True

int[2,4] is assignable from int[2,2]? True

int[2,2] is assignable from int[10]? False

int[2,2] is assignable from int[3,3,3]? False

int[3,3,3] is assignable from int[2,2]? False

See Also

System.Type Class, System Namespace

Type.IsByRefImpl Method

protected abstract bool IsByRefImpl();

Summary

When overridden in a derived class, implements the System.Type.IsByRef property and determines whether the Type is passed by reference.

Return Value

true if the Type is passed by reference; otherwise, false .

Description

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsCOMObjectImpl Method

protected abstract bool IsCOMObjectImpl();

Summary

Reserved.

Return Value

false

Description

This abstract method is required to be present for legacy implementations. Conforming implementations are permitted to throw the NotSupportedException as their implementation.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsInstanceOfType Method

public virtual bool IsInstanceOfType(object o);

Summary

Determines whether the specified object is an instance of the current Type.

Parameters

o
The object to compare with the current Type.

Return Value

true if either of the following statements is true; otherwise false .

If o is a null reference or if the current instance is an open generic type (that is, System.Type.ContainsGenericParameters returns true ) returns false .

Description

As described above.

[Note: A constructed type is not an instance of its generic type definition.

]

[Behaviors: As described above.]

Example

The following example demonstrates the System.Type.IsInstanceOfType(System.Object) method.

using System;
public interface IFoo { }
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsInstanceTest {
 public static void Main() {
 Type ifooType=typeof(IFoo);
 MyClass mc = new MyClass();
 Type mcType = mc.GetType();
 MyClass mdc = new MyDerivedClass();
 Type mdcType = mdc.GetType();
 int [] array = new int [10];
 Type arrayType = typeof(Array);
 Console.WriteLine("int[] is instance of Array? {0}", arrayType.IsInstanceOfType(array));
 Console.WriteLine("myclass instance is instance of MyClass? {0}", mcType.IsInstanceOfType(mc));
 Console.WriteLine("myderivedclass instance is instance of MyClass? {0}",  mcType.IsInstanceOfType(mdc));
 Console.WriteLine("myclass instance is instance of IFoo? {0}", ifooType.IsInstanceOfType(mc));
 Console.WriteLine("myderivedclass instance is instance of IFoo? {0}",  ifooType.IsInstanceOfType(mdc));
 }
}
The output is

int[] is instance of Array? True

myclass instance is instance of MyClass? True

myderivedclass instance is instance of MyClass? True

myclass instance is instance of IFoo? True

myderivedclass instance is instance of IFoo? True

See Also

System.Type Class, System Namespace

Type.IsPointerImpl Method

protected abstract bool IsPointerImpl();

Summary

When overridden in a derived class, implements the System.Type.IsPointer property and determines whether the Type is a pointer.

Return Value

true if the Type is a pointer; otherwise, false .

Description

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsPrimitiveImpl Method

protected abstract bool IsPrimitiveImpl();

Summary

When overridden in a derived class, implements the System.Type.IsPrimitive property and determines whether the Type is one of the primitive types.

Return Value

true if the Type is one of the primitive types; otherwise, false .

Description

[Behaviors: This method returns true if the underlying type of the current instance is one of the following: Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Char, Double, and Single.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsSubclassOf Method

public virtual bool IsSubclassOf(Type c);

Summary

Determines whether the current Type derives from the specified Type .

Parameters

c
The Type to compare with the current Type.

Return Value

true if c and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwise false . Returns false if c and the current Type represent the same class.

Description

Interfaces are not considered.

If the current instance represents an unassigned type parameter of a generic type or method, it derives from its class constraint, or from Object if it has no class constraint.

Example

The following example demonstrates the System.Type.IsSubclassOf(System.Type) method.

using System;
public interface IFoo { }
public interface IBar:IFoo{}
public class MyClass : IFoo {}
public class MyDerivedClass : MyClass {}
class IsSubclassTest {
 public static void Main() {
 Type ifooType = typeof(IFoo);
 Type ibarType = typeof(IBar);
 MyClass mc = new MyClass();
 Type mcType = mc.GetType();
 MyClass mdc = new MyDerivedClass();
 Type mdcType = mdc.GetType();
 int [] array = new int [10];
 Type arrayOfIntsType = array.GetType();
 Type arrayType = typeof(Array);
 
 Console.WriteLine("Array is subclass of int[]? {0}", arrayType.IsSubclassOf(arrayOfIntsType));
 Console.WriteLine("int [] is subclass of Array? {0}", arrayOfIntsType.IsSubclassOf(arrayType));
 Console.WriteLine("IFoo is subclass of IBar? {0}", ifooType.IsSubclassOf(ibarType));
 Console.WriteLine("myclass is subclass of MyClass? {0}", mcType.IsSubclassOf(mcType));
 Console.WriteLine("myderivedclass is subclass of MyClass? {0}", mdcType.IsSubclassOf(mcType));
 Console.WriteLine("IBar is subclass of IFoo? {0}", ibarType.IsSubclassOf(ifooType)); 
  }
}
The output is

Array is subclass of int[]? False

int [] is subclass of Array? True

IFoo is subclass of IBar? False

myclass is subclass of MyClass? False

myderivedclass is subclass of MyClass? True

IBar is subclass of IFoo? False

See Also

System.Type Class, System Namespace

Type.MakeArrayType() Method

public virtual Type MakeArrayType()

Summary

Returns a Type object representing a one-dimensional array type whose element type is the current type, with a lower bound of zero.

Return Value

A Type object representing a one-dimensional array type whose element type is the current type, with a lower bound of zero.

Description

This method provides a way to generate an array type with any possible element type, including generic types.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.MakeArrayType(int) Method

public virtual Type MakeArrayType(int rank)

Summary

Returns a Type object representing an array of the current type, with the specified number of dimensions.

Parameters

rank
The number of dimensions for the array.

Return Value

A Type object representing an array of the current type, with the specified number of dimensions.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptionrank is invalid (being less than 1, for example).

Description

This method provides a way to generate an array with any possible element type, including generic types.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.MakeByRefType Method

public virtual Type MakeByRefType()

Summary

Returns a Type object that represents the current type when passed as a byref parameter.

Return Value

A Type object that represents the current type when passed as a byref parameter.

Description

This method provides a way to generate a byref type for any type.

[Note: Using ilasm syntax, if the current Type object represents int32 , this method returns a Type object representing int32& .]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.MakeGenericType Method

public override Type MakeGenericType(System.Type[] typeArguments)

Summary

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.

The current type shall be a generic type definition.

Parameters

typeArguments
An array of types to be substituted for the type parameters of the current generic type definition.

Return Value

A Type representing the constructed type formed by substituting the elements of typeArguments for the type parameters of the current generic type definition.

Exceptions

Exception TypeCondition
ArgumentExceptionThe number of elements in typeArguments is not the same as the number of type parameters of the current generic type definition.

-or-

An element of typeArguments does not satisfy the constraints specified for the corresponding type parameter of the current generic type definition.

ArgumentNullExceptiontypeArguments is null .

-or-

An element of typeArguments is null .

InvalidOperationExceptionThe current type does not represent the definition of a generic type. That is, System.Type.IsGenericTypeDefinition returns false .

Description

This method allows you to write code that assigns specific types to the type parameters of a generic type definition, thus creating a Type object that represents a particular constructed type. You can use this Type object to create runtime instances of the constructed type.

The Type object returned by this method is the same as that obtained by calling the System.Object.GetType method of the resulting constructed type, or the System.Object.GetType method of any constructed type that was created from the same generic type using the same type arguments.

[Note: An array type whose element type is a generic type is not itself a generic type. Thus, you cannot call this method to bind an array type. To bind a type argument to this type, call System.Type.GetElementType to obtain the generic type, then this method to bind the type argument to the generic type, and, finally, System.Type.MakeArrayType to create the array type. ]

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Example

The following example uses System.Type.GetType and System.Type.MakeGenericType to create a constructed type from the generic System.Collections.Generic.Dictionary type. The constructed type represents a System.Collections.Generic.Dictionary of Test objects with string keys.

using System;
using System.Reflection;
using System.Collections.Generic;

public class Test
{
	public static void Main()
	{
		Console.WriteLine("\n--- Create a constructed type from the generic  Dictionary type.");

		// Create a type object representing the generic Dictionary 
		// type.       
		Type generic = Type.GetType("System.Collections.Generic.Dictionary");

		DisplayTypeInfo(generic);

		// Create an array of types to substitute for the type
		// parameters of Dictionary. The key is of type string, and
		// the type to be contained in the Dictionary is Test.
		Type[] typeArgs = { typeof(string), typeof(Test) };
		Type constructed = generic.MakeGenericType(typeArgs);

		DisplayTypeInfo(constructed);

		// Compare the type objects obtained above to type objects
		// obtained using typeof() and GetGenericTypeDefinition().
		Console.WriteLine("\n--- Compare types obtained by different methods:");

		Type t = typeof(Dictionary<string, Test>);

		Console.WriteLine("\tAre the constructed types equal? {0}", t == constructed);
		Console.WriteLine("\tAre the generic types equal? {0}", t.GetGenericTypeDefinition() == generic);
	}

	private static void DisplayTypeInfo(Type t)
	{
		Console.WriteLine("\n{0}", t);
		Console.WriteLine("\tIs this a generic type definition? {0}", t.IsGenericTypeDefinition);
		Console.WriteLine("\tDoes it have generic type arguments? {0}", t.HasGenericArguments);

		Type[] typeArguments = t.GetGenericArguments();

		Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
		foreach (Type tParam in typeArguments)
		{
			Console.WriteLine("\t\t{0}", tParam);
		}
	}
}

/* This example produces the following output:

--- Create a constructed type from the generic Dictionary type.

System.Collections.Generic.Dictionary[KeyType,ValueType]
        Is this a generic type definition? True
        Does it have generic type arguments? True
        List type arguments (2):
                K
                V

System.Collections.Generic.Dictionary[System.String, Test]
        Is this a generic type definition? False
        Does it have generic type arguments? True
        List type arguments (2):
                System.String
                Test

--- Compare types obtained by different methods:
        Are the constructed types equal? True
        Are the generic types equal? True
 */

Library

Reflection

See Also

System.Type Class, System Namespace

Type.MakePointerType Method

public virtual Type MakePointerType()

Summary

Returns a Type object that represents the type of an unmanaged pointer to the current type.

Return Value

A Type object that represents the type of an unmanaged pointer to the current type.

Description

This method provides a way to generate an unmanaged pointer type for types computed at runtime.

[Note: Using ilasm syntax, if the current Type object represents int32 , this method returns a Type object representing int32* .]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.ToString Method

public override string ToString();

Summary

Returns a String representation of the current Type.

Return Value

Returns System.Type.FullName .

Description

[Note: This method overrides System.Object.ToString .]

If the current instance represents a generic type, the type and its type arguments are qualified by namespace and by nested type, but not by assembly. If the current instance represents an unassigned type parameter of a generic type or method, this method returns the unqualified name of the type parameter.

See Also

System.Type Class, System Namespace

Type.Delimiter Field

public static readonly char Delimiter;

Summary

Specifies the character that separates elements in the fully qualified name of a Type .

Description

This field is read-only.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.EmptyTypes Field

public static readonly Type[] EmptyTypes;

Summary

Returns an empty array of type Type .

Description

This field is read-only.

The empty Type array returned by this field is used to specify that lookup methods in the Type class, such as System.Type.GetMethod(System.String,System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) and System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]), search for members that do not take parameters. [Note: For example, to locate the public instance constructor that takes no parameters, invoke System.Type.GetConstructor(System.Reflection.BindingFlags,System.Reflection.Binder,System.Reflection.CallingConventions,System.Type[],System.Reflection.ParameterModifier[]) (System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null , System.Type.EmptyTypes, null ).]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.Missing Field

public static readonly object Missing;

Summary

Represents a missing value in the Type information.

Description

This field is read-only.

Use the Missing field for invocation through reflection to ensure that a call will be made with the default value of a parameter as specified in the metadata. [Note: If the Missing field is specified for a parameter value and there is no default value for that parameter, a ArgumentException is thrown.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.Assembly Property

public abstract Assembly Assembly { get; }

Summary

Gets the Assembly in which the type is declared. For generic types, gets the Assembly that contains the generic type definition.

Property Value

A Assembly instance that describes the assembly containing the current type. For generic types, the instance describes the assembly that contains the definition of the generic type.

Description

If the current instance represents a generic type, this property returns the assembly in which the type was defined. For example, suppose you create an assembly named MyGenerics.dll that contains a class named MyGenericStack<T> . If you create an instance of MyGenericStack<int> in another assembly, the System.Type.Assembly property for the constructed type returns a Assembly that represents MyGenerics.dll.

Similarly, if the current instance represents a generic parameter T, this property returns the assembly that contains the generic type definition that defines T.

[Behaviors: This property is read-only.]

Library

RuntimeInfrastructure

See Also

System.Type Class, System Namespace

Type.AssemblyQualifiedName Property

public abstract string AssemblyQualifiedName { get; }

Summary

Gets the fully qualified name of the type represented by the current instance including the name of the assembly from which the Type was loaded.

Property Value

The assembly-qualified name of the Type, including the name of the assembly from which the Type was loaded. If the current Type object represents a generic parameter, this property returns null .

Description

[Behaviors: This property is read-only.

Compilers emit the simple name of a nested class, and reflection constructs a mangled name when queried, in accordance with the following conventions.

DelimiterMeaning
Backslash (\)Escape character.
Comma (,)Precedes the Assembly name.
Plus sign (+)Precedes a nested class.
Period (.)Denotes namespace identifiers.
Square brackets ([])After a type name, denotes an array of that type.

-or-

For a generic type, encloses the entire generic type argument list.

-or-

Within a type argument list, encloses an assembly-qualified type.

[Note: For example, the fully qualified name for a class might look like this:

TopNamespace.SubNameSpace.ContainingClass+NestedClass,MyAssembly

If the namespace were TopNamespace.Sub+Namespace, then the string would have to precede the plus sign (+) with an escape character (\) to prevent it from being interpreted as a nesting separator. Reflection emits this string as follows:

TopNamespace.Sub\+Namespace.ContainingClass+NestedClass,MyAssembly

A "++" becomes "\+\+", and a "\" becomes "\\".

]

Type names are permitted to include trailing characters that denote additional information about the type, such as whether the type is a reference type, a pointer type or an array type. To retrieve the type name without these trailing characters, use t.GetElementType().ToString(), where t is the type.

Spaces are significant in all type name components except the assembly name. In the assembly name, spaces before the ',' separator are significant, but spaces after the ',' separator are ignored.

Generic arguments of generic types are themselves fully qualified. For example, the output from the following C# program, if compiled to an assembly called Try64

using System;
using System.Reflection;

class MyTest {
  public static void Main(String[] args) {
    Type b = typeof(B<string,object>);
    Console.WriteLine(b.AssemblyQualifiedName);
  }
}
public class B<T,U> { }
is as follows:

B`2[[System.String, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Try64, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
]

[Usage: The name returned by this method can be persisted and later used to load the Type. To search for and load a Type, use System.Type.GetType(System.String,System.Boolean,System.Boolean) either with the type name only or with the assembly qualified type name. System.Type.GetType(System.String,System.Boolean,System.Boolean) with the type name only will look for the Type in the caller's assembly and then in the System assembly. System.Type.GetType(System.String,System.Boolean,System.Boolean) with the assembly qualified type name will look for the Type in any assembly.

]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.Attributes Property

public TypeAttributes Attributes { get; }

Summary

Gets the attributes associated with the type represented by the current instance.

Property Value

A TypeAttributes object representing the attribute set of the Type.

Description

This property is read-only.

If the current instance represents a generic type, this property returns the attributes of the generic type definition.

If the current instance represents a generic type parameter T, the System.Reflection.TypeAttributes returned by this property includes System.Reflection.TypeAttributes.Abstract, System.Reflection.TypeAttributes.AnsiClass, System.Reflection.TypeAttributes.AutoLayout, System.Reflection.TypeAttributes.Class, System.Reflection.TypeAttributes.Public, and System.Reflection.TypeAttributes.Sealed. These are arbitrary choices which have no meaning in the context of a type parameter.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.BaseType Property

public abstract Type BaseType { get; }

Summary

Gets the base Type of the current Type .

Property Value

A Type object representing the type from which the current Type directly inherits, or null if the current Type represents the Object class.

Description

The base type is the type from which the current type directly inherits. Object is the only type that does not have a base type, therefore null is returned as the base type of Object .

Interfaces inherit from Object and from zero or more base interfaces; therefore, the base type of an interface is considered to be Object .

If the current instance represents a constructed generic type, the base type reflects the generic arguments.

If the current instance represents an unassigned type parameter, System.Type.BaseType returns the base class type constraint declared for that parameter, or Object if no base class type constraint was declared.

[Behaviors: This property is read-only.]

Example

The following example demonstrates using the System.Type.BaseType property.

using System;
class TestType {
 public static void Main() {
 Type t = typeof(int);
 Console.WriteLine("{0} inherits from {1}", t,t.BaseType);
 }
}
The output is

System.Int32 inherits from System.ValueType

See Also

System.Type Class, System Namespace

Type.ContainsGenericParameters Property

public virtual bool ContainsGenericParameters { get; }

Summary

Gets a value that indicates whether a Type object contains unassigned generic parameters.

Property Value

true if a Type object contains unassigned generic parameters; otherwise false .

Description

In order to create an instance of a generic type, there must be no generic type definitions or open constructed types in the type arguments. For other constructed types, such as arrays and managed pointers, the types from which they are constructed must be able to be instantiated. If the System.Type.ContainsGenericParameters property returns true , the type cannot be instantiated.

The System.Type.ContainsGenericParameters property searches recursively for type parameters. For example, it returns true for an array whose element type is A<T> , even though the array type itself is not generic. Contrast this with the behavior of the System.Type.IsGenericType property, which returns false for arrays.

For a set of example classes and a table showing the values of the System.Type.ContainsGenericParameters property, see the System.Type.IsGenericType property description.

[Behaviors: This property is read-only.

]

Example

For an example of using this method, see the example for System.Type.GenericParameterPosition.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.DeclaringMethod Property

public virtual MethodBase DeclaringMethod { get; }

Summary

If the current Type represents a type parameter of a generic method, gets a MethodInfo that represents the declaring method.

Property Value

If the current Type represents a type parameter of a generic method, a MethodInfo that represents the declaring method; otherwise null .

Description

The declaring method is a generic method definition. That is, if System.Type.DeclaringMethod does not return null , then DeclaringMethod.IsGenericMethodDefinition returns true .

The System.Type.DeclaringType and System.Type.DeclaringMethod properties identify the generic type definition or generic method definition where the generic type parameter was originally defined:

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

[Behaviors: This property is read-only.

]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.DeclaringType Property

public virtual Type DeclaringType { get; }

Summary

Gets the type that declares the type represented by the current instance.

Property Value

The Type object for the class that declares the type represented by the current instance. If the type is a nested type, this property returns the enclosing type; otherwise, returns the current instance.

Description

[Note: This property implements the abstract property inherited from System.Reflection.MemberInfo.]

If the current Type represents a type parameter of a generic type or method definition, the System.Type.DeclaringType and System.Type.DeclaringMethod properties identify the generic type definition or generic method definition where the generic type parameter was originally defined:

For a type parameter of a generic method, this property returns the type that contains the generic method definition.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.DefaultBinder Property

public static Binder DefaultBinder { get; }

Summary

Gets the default binder used by the system.

Property Value

The default Binder used by the system.

Description

This property is read-only.

Reflection models the accessibility rules of the common type system. For example, if the caller is in the same assembly, the caller does not need special permissions for internal members. Otherwise, the caller needs ReflectionPermission . This is consistent with lookup of members that are protected, private, and so on.

[Note: The general principle is that System.Reflection.Binder.ChangeType(System.Object,System.Type,System.Globalization.CultureInfo) typically performs only widening coercions, which never lose data. An example of a widening coercion is coercing a value that is a 32-bit signed integer to a value that is a 64-bit signed integer. This is distinguished from a narrowing coercion, which can lose data. An example of a narrowing coercion is coercing a 64-bit signed integer to a 32-bit signed integer. ]

The following table lists the coercions performed by the default binder's implementation of ChangeType .

Source TypeTarget Type
Any typeIts base type.
Any typeThe interface it implements.
CharUnt16, UInt32, Int32, UInt64, Int64, Single, Double
ByteChar, Unt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double
SByteInt16, Int32, Int64, Single, Double
UInt16UInt32, Int32, UInt64, Int64, Single, Double
Int16Int32, Int64, Single, Double
UInt32UInt64, Int64, Single, Double
Int32Int64, Single, Double
UInt64Single, Double
Int64Single, Double
SingleDouble
Non-referenceBy-reference.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.FullName Property

public abstract string FullName { get; }

Summary

Gets the fully qualified name of the type represented by the current instance.

Property Value

A String containing the fully qualified name of the Type.

Description

[Note: For example, the fully qualified name of the C# string type is "System.String".]

If the current instance represents a generic type, the type arguments in the string returned by System.Type.FullName are qualified by their assembly, version, and so on, even though the string representation of the generic type itself is not qualified by assembly. Thus, t.FullName + ", " + t.Assembly.FullName produces the same result as t.AssemblyQualifiedName , as with types that are not generic.

If the current instance represents an unassigned type parameter of a generic type, this property returns null .

[Behaviors: This property is read-only.]

Example

The following example demonstrates using the System.Type.FullName property.

using System;
class TestType {
 public static void Main() {
 Type t = typeof(Array);
 Console.WriteLine("Full name of Array type is {0}",t.FullName);
 }
}
The output is

Full name of Array type is System.Array

See Also

System.Type Class, System Namespace

Type.GenericParameterAttributes Property

public virtual GenericParameterAttributes GenericParameterAttributes { get; }

Summary

Gets a combination of GenericParameterAttributes flags that describe the variance and special constraints of the current generic type parameter.

Property Value

A GenericParameterAttributes value that describes the variance and special constraints of the current generic type parameter.

Exceptions

Exception TypeCondition
InvalidOperationExceptionThe current Type object is not a generic type parameter. That is, the System.Type.IsGenericParameter property returns false .

Description

This property is read-only.

The value of this property contains flags that describe whether the current generic type parameter is variant, and flags that describe any special constraints. Use System.GenericParameterAttributes.VarianceMask to select the variance flags, and System.GenericParameterAttributes.SpecialConstraintMask to select the constraint flags. Use System.Reflection.GetGenericParameterConstraints to get the type constraints.

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.GenericParameterPosition Property

public virtual int GenericParameterPosition { get; }

Summary

For a Type object that represents a type parameter of a generic type or generic method, gets the position of the type parameter in the type parameter list of the generic type or generic method.

Property Value

A zero-based integer representing the position of a type parameter in the type parameter list of the generic type or generic method that declared the parameter.

Exceptions

Exception TypeCondition
InvalidOperationExceptionThe current type does not represent a type parameter. That is, System.Type.IsGenericParameter returns false .

Description

This read-only property returns the position of a type parameter in the parameter list of the generic type definition or generic method definition where the type parameter was originally defined. The System.Type.DeclaringType and System.Type.DeclaringMethod properties identify the generic type or method definition:

To provide the correct context for the value of the System.Type.GenericParameterPosition property, it is necessary to identify the generic type or method a type parameter belongs to. For example, consider the return value of the generic method GetSomething in the following C# code:

public class B<T, U> { }
 public class A<V>
 {
     public B<V, X> GetSomething<X>()
     {
         return new Base<V, X>();
     }
 }
The type returned by GetSomething depends on the type arguments supplied to class A and GetSomething itself. You can obtain a MethodInfo for GetSomething and from that you can obtain the return type. When you examine the type parameters of the return type, System.Type.GenericParameterPosition returns zero for both. The position of V is zero because V is the first type parameter in the type parameter list for class A . The position ofX is zero because X is the first type parameter in the type parameter list for GetSomething .

[Note: Calling the System.Type.GenericParameterPosition property causes an exception if the current Type does not represent a type parameter. When you examine the type arguments of an open constructed type, use the System.Type.IsGenericParameter property to tell which are type parameters and which are types. The System.Type.IsGenericParameter property returnstrue for a type parameter; you can then use the System.Type.GenericParameterPosition method to obtain its position, and the System.Type.DeclaringMethod and System.Type.DeclaringType properties to determine the generic method or type definition that defines it.

]

Example

The following example defines a generic class with two type parameters, and a generic class that derives from it. The base class of the derived type has one unbound type parameter and one type parameter bound to Int32. The example displays information about these generic classes, including the positions reported by System.Type.GenericParameterPosition.

using System;
using System.Reflection;
using System.Collections.Generic;
// Define a base class with two type parameters.
public class Base<T, U> { }

// Define a derived class. The derived class inherits from a constructed
// class that meets the following criteria:
//   (1) Its generic type definition is Base<T, U>.
//   (2) It specifies int for the first type parameter.
//   (3) For the second type parameter, it uses the same type that is used
//       for the type parameter of the derived class.
// Thus, the derived class is a generic type with one type parameter, but
// its base class is an open constructed type with one type argument and
// one type parameter.
public class Derived<V>: Base<int,V> { }

public class Test
{
	public static void Main()
	{
		Console.WriteLine("\n--- Display a generic type and the open constructed");
		Console.WriteLine("    type from which it is derived.");

		// Create a Type object representing the generic type Derived.
		//
		Type derivedType = Type.GetType("Derived");

		DisplayGenericTypeInfo(derivedType);

		// Display its open constructed base type.
		DisplayGenericTypeInfo(derivedType.BaseType);
	}

	private static void DisplayGenericTypeInfo(Type t)
	{
		Console.WriteLine("\n{0}", t);
		Console.WriteLine("\tIs this a generic type definition? {0}", t.IsGenericTypeDefinition);
		Console.WriteLine("\tDoes it have generic arguments? {0}", t.HasGenericArguments);
		Console.WriteLine("\tDoes it have unbound generic parameters? {0}", t.ContainsGenericParameters);
		if (t.HasGenericArguments)
		{
			// If the type is a generic type definition or a 
			// constructed type, display the type arguments.
			//
			Type[] typeArguments = t.GetGenericArguments();

			Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
			foreach (Type tParam in typeArguments)
			{
				// IsGenericParameter is true only for generic type
				// parameters.
				//
				if (tParam.IsGenericParameter)
				{
					Console.WriteLine("\t\t{0}  (unbound - parameter position {1})", tParam, tParam.GenericParameterPosition);
				}
				else
				{
					Console.WriteLine("\t\t{0}", tParam);
				}
			}
		}
		else
		{
			Console.WriteLine("\tThis is not a generic or constructed type.");
		}
	}
}

/* This example produces the following output:

--- Display a generic type and the open constructed
    type from which it is derived.

Derived[V]
        Is this a generic type definition? True
        Does it have generic arguments? True
        Does it have unbound generic parameters? True
        List type arguments (1):
                V  (unbound - parameter position 0)

Base[System.Int32, V]
        Is this a generic type definition? False
        Does it have generic arguments? True
        Does it have unbound generic parameters? True
        List type arguments (2):
                System.Int32
                V  (unbound - parameter position 0)
 */

Library

Reflection

See Also

System.Type Class, System Namespace

Type.HasElementType Property

public bool HasElementType { get; }

Summary

Gets a Boolean value indicating whether the type represented by the current instance encompasses or refers to another type; that is, whether the current Type is an array, a pointer, or is passed by reference.

Property Value

true if the Type is an array, a pointer, or is passed by reference; otherwise, false .

Description

This property is read-only.

[Note: For example, System.Type.GetType(System.String,System.Boolean,System.Boolean)("Int32 []").HasElementType returns true , but System.Type.GetType(System.String,System.Boolean,System.Boolean)("Int32 ").HasElementType returns false . System.Type.HasElementType also returns true for "Int32*" and "Int32&".]

If the current instance represents a generic type, or a type parameter of a generic type or method, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsAbstract Property

public bool IsAbstract { get; }

Summary

Gets a Boolean value indicating whether the type represented by the current instance is abstract and is required to be overridden.

Property Value

true if the Type is abstract; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property always returns true . This is because it is not possible to create an instance of a generic type parameter.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsAnsiClass Property

public virtual bool IsAnsiClass { get; }

Summary

Indicates whether the type attribute System.Reflection.TypeAttributes.AnsiClass is selected for the current type.

Property Value

true if the type attribute System.Reflection.TypeAttributes.AnsiClass is selected for the current type; otherwise, false .

Description

This property is read-only.

If the current Type represents a generic type, this property applies to the definition of the type. If the current Type represents a type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsArray Property

public bool IsArray { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents an array.

Property Value

true if the current Type represents an array; otherwise false .

Description

This property is read-only.

This property returns true for an array of objects, but not for the Array type itself, which is a class.

If the current instance represents a generic type, or a type parameter of a generic type or method, this property returns false .

Example

The following example demonstrates using the System.Type.IsArray property.

using System;
class TestType {
 public static void Main() {
 int [] array = {1,2,3,4};
 Type at = typeof(Array);
 Type t = array.GetType();
 Console.WriteLine("Type is {0}. IsArray? {1}", at, at.IsArray);
 Console.WriteLine("Type is {0}. IsArray? {1}", t, t.IsArray);
 }
}
The output is

Type is System.Array. IsArray? False

Type is System.Int32[]. IsArray? True

See Also

System.Type Class, System Namespace

Type.IsAutoClass Property

public virtual bool IsAutoClass { get; }

Summary

Indicates whether the type attribute System.Reflection.TypeAttributes.AutoClass is selected for the current type.

Property Value

true if the type attribute System.Reflection.TypeAttributes.AutoClass is selected for the current type; otherwise, false .

Description

This property is read-only.

If the current Type represents a generic type, this property applies to the definition of the type. If the current Type represents a type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsAutoLayout Property

public bool IsAutoLayout { get; }

Summary

Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.AutoLayout is specified for the Type.

Property Value

true if the type layout attribute System.Reflection.TypeAttributes.AutoLayout is specified for the current Type; otherwise, false .

Description

This property is read-only.

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

[Note: The System.Reflection.TypeAttributes.AutoLayout attribute specifies that the system selects the layout the objects of the type. Types marked with this attribute indicate that the system will choose the appropriate way to lay out the type; any layout information that might have been specified is ignored.

]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsByRef Property

public bool IsByRef { get; }

Summary

Gets a Boolean value indicating whether the Type is passed by reference.

Property Value

true if the Type is passed by reference; otherwise, false .

Description

This property is read-only.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsClass Property

public bool IsClass { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents a class.

Property Value

true if the current Type represents a class; otherwise false .

Description

This property is read-only.

Note that this property returns true for Type instances representing Enum and ValueType .

If the current instance represents a generic type, this property returns true if the generic type definition is a class definition (that is, it does not define an interface or a value type).

If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

See Also

System.Type Class, System Namespace

Type.IsEnum Property

public bool IsEnum { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents an enumeration.

Property Value

true if the current Type represents an enumeration; otherwise false .

Description

This property is read-only.

This property returns true for an enumeration, but not for the Enum type itself, which is a class.

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

Example

The following example demonstrates using the System.Type.IsEnum property.

using System;
public enum Color {
Red, Blue, Green
}
class TestType {
 public static void Main() {
 Type colorType = typeof(Color);
 Type enumType = typeof(Enum);
 Console.WriteLine("Color is enum ? {0}", colorType.IsEnum);
 Console.WriteLine("Color is valueType? {0}", colorType.IsValueType);
 Console.WriteLine("Enum is enum Type? {0}", enumType.IsEnum);
 Console.WriteLine("Enum is value? {0}", enumType.IsValueType);
 }
}
The output is

Color is enum ? True

Color is valueType? True

Enum is enum Type? False

Enum is value? False

See Also

System.Type Class, System Namespace

Type.IsExplicitLayout Property

public bool IsExplicitLayout { get; }

Summary

Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.ExplicitLayout is specified for the Type.

Property Value

true if the type layout attribute System.Reflection.TypeAttributes.ExplicitLayout is specified for the current Type; otherwise, false .

Description

This property is read-only.

[Note: Types marked with the System.Reflection.TypeAttributes.ExplicitLayout attribute cause the system to ignore field sequence and to use the explicit layout rules provided, in the form of field offsets, overall class size and alignment.

]

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsGenericParameter Property

public virtual bool IsGenericParameter { get; }

Summary

Gets a value that indicates whether the current type represents a type parameter of a generic type or method.

Property Value

true if the current object represents a type parameter of a generic type or method; otherwise false .

Description

This property is read-only.

Use this property to distinguish between type parameters and type arguments. When you call System.Type.GetGenericArguments to obtain the type arguments of a generic type, some elements of the array might be specific types (type arguments) and others might be type parameters.System.Type.IsGenericParameter returns false for the types and true for the type parameters.

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

Example

For an example of using this method, see the example for System.Type.GenericParameterPosition.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsGenericType Property

public virtual bool IsGenericType { get; }

Summary

Gets a value that indicates whether the current type has type arguments, and is therefore a generic type.

Property Value

true if the current type has type arguments; otherwise false .

Description

Use this property to determine whether a Type object represents a generic type. Use the System.Type.ContainsGenericParameters property to determine whether a Type object represents an open constructed type or a closed constructed type.

[Note: The System.Type.HasGenericArguments property returns false if the immediate type is not generic. ]

The following table summarizes the invariant conditions for common terms used in generic reflection.

TermInvariant
generic type definitionThe System.Type.IsGenericTypeDefinition property is true .

Defines a generic type. A constructed type is created by calling the System.Type.MakeGenericType(System.Type[]) method on a Type object that represents a generic type definition, and specifying an array of type arguments.

System.Type.MakeGenericType(System.Type[]) can be called only on generic type definitions.

Any generic type definition is a generic type, but the converse is not true.

generic typeThe System.Type.IsGenericType property is true .

Can be a generic type definition, an open constructed type, or a closed constructed type.

Note that an array type whose element type is generic is not itself a generic type. The same is true of a Type object representing a pointer to a generic type.

open constructed typeThe System.Type.ContainsGenericParameters property is true .

It is not possible to create an instance of an open constructed type.

Note that not all open constructed types are generic, such as an array type whose element type is a generic type definition.

closed constructed typeThe System.Type.ContainsGenericParameters property is false .

When examined recursively, the type has no unassigned generic parameters. The containing type or method has no generic type parameters, and, recursively, no type arguments have unassigned generic type parameters.

generic type parameterThe System.Type.IsGenericParameter property is true .

In a generic type definition, a placeholder for a type that will be assigned later.

generic type argumentCan be any type, including a generic type parameter.

Type arguments are specified as an array of Type objects passed to the System.Type.MakeGenericType(System.Type[]) method when creating a constructed generic type. If instances of the resulting type are to be created, the System.Type.ContainsGenericParameters property must be false for all the type arguments.

[Behaviors: This property is read-only.

]

Example

For an example of using this method, see the example for System.Type.MakeGenericType.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsGenericTypeDefinition Property

public virtual bool IsGenericTypeDefinition { get; }

Summary

Gets a value that indicates whether the current object represents the definition of a generic type, or whether one or more of its type parameters has been specified.

Property Value

true if the current object represents the definition of a generic type, none of whose type parameters have been bound to specific types; otherwise false .

Description

This property is read-only.

Use this property to determine whether type arguments have been specified for any of the type parameters of a generic type. If type arguments have been specified (that is, bound to the corresponding type parameters), this property returns false .

For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.

[Note: An open generic type can have type parameters even if types have been specified for its type parameters.

]

Example

For an example of using this method, see the example for System.Type.MakeGenericType..

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsImport Property

public bool IsImport { get; }

Summary

Gets a Boolean value indicating whether the Type was imported from another class.

Property Value

true if the Type was imported from another class; otherwise, false .

Description

This property is read-only.

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsInterface Property

public bool IsInterface { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents an interface.

Property Value

true if the current Type represents an interface; otherwise false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

See Also

System.Type Class, System Namespace

Type.IsLayoutSequential Property

public bool IsLayoutSequential { get; }

Summary

Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.SequentialLayout is specified for the Type.

Property Value

true if the type layout attribute System.Reflection.TypeAttributes.SequentialLayout is specified for the current Type; otherwise, false .

Description

This property is read-only.

[Note: The System.Reflection.TypeAttributes.SequentialLayout attribute is used to indicate that the system is to preserve field order as emitted, but otherwise the specific offsets are calculated based on the type of the field; these might be shifted by explicit offset, padding, or alignment information.

]

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsMarshalByRef Property

public bool IsMarshalByRef { get; }

Summary

Gets a Boolean value indicating whether the current type is marshaled by reference.

Property Value

true if the Type is marshaled by reference; otherwise, false .

Description

This property is read-only.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedAssembly Property

public bool IsNestedAssembly { get; }

Summary

Gets a Boolean value indicating whether the current Type is nested and visible only within its own assembly.

Property Value

true if the Type is nested and visible only within its own assembly; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedFamANDAssem Property

public bool IsNestedFamANDAssem { get; }

Summary

Gets a Boolean value indicating whether the current Type is nested and visible only to classes that belong to both its own family and its own assembly.

Property Value

true if the Type is nested and visible only to classes that belong to both its own family and its own assembly; otherwise, false .

Description

This property is read-only.

A Type object's family is defined as all objects of the exact same Type and of its subclasses.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedFamORAssem Property

public bool IsNestedFamORAssem { get; }

Summary

Gets a Boolean value indicating whether the current Type is nested and visible only to classes that belong to either its own family or to its own assembly.

Property Value

true if the Type is nested and visible only to classes that belong to its own family or to its own assembly; otherwise, false .

Description

This property is read-only.

A Type object's family is defined as all objects of the exact same Type and of its subclasses.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedFamily Property

public bool IsNestedFamily { get; }

Summary

Gets a Boolean value indicating whether the current Type is nested and visible only within its own family.

Property Value

true if the Type is nested and visible only within its own family; otherwise, false .

Description

This property is read-only.

A Type object's family is defined as all objects of the exact same Type and of its subclasses.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedPrivate Property

public bool IsNestedPrivate { get; }

Summary

Gets a Boolean value indicating whether the current Type is nested and declared private.

Property Value

true if the Type is nested and declared private; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNestedPublic Property

public bool IsNestedPublic { get; }

Summary

Gets a Boolean value indicating whether the current Type is a public nested class.

Property Value

true if the class is nested and declared public; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsNotPublic Property

public bool IsNotPublic { get; }

Summary

Gets a Boolean value indicating whether the top-level Type is not declared public.

Property Value

true if the top-level Type is not declared public; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsPointer Property

public bool IsPointer { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents a pointer.

Property Value

This property is read-only.

true if the current Type represents a pointer; otherwise false .

Description

This property is read-only.

If the current instance represents a generic type, or a type parameter of a generic type or method, this property always returns false .

See Also

System.Type Class, System Namespace

Type.IsPrimitive Property

public bool IsPrimitive { get; }

Summary

Gets a Boolean value indicating whether the current Type is one of the primitive types.

Property Value

true if the Type is one of the primitive types; otherwise, false .

Description

This property is read-only.

The primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Char, Double, and Single.

If the current instance represents a generic type, or a type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsPublic Property

public bool IsPublic { get; }

Summary

Gets a Boolean value indicating whether the top-level Type is declared public.

Property Value

true if the top-level Type is declared public; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns true .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsSealed Property

public bool IsSealed { get; }

Summary

Gets a Boolean value indicating whether the current Type is declared sealed.

Property Value

true if the Type is declared sealed; otherwise, false .

Description

This property is read-only.

If the current instance represents an unassigned type parameter of a generic type, this property returns true .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsSpecialName Property

public bool IsSpecialName { get; }

Summary

Gets a Boolean value indicating whether the current Type has a name that requires special handling.

Property Value

true if the Type has a name that requires special handling; otherwise, false .

Description

This property is read-only.

[Note: Names that begin with or contain an underscore character (_) are examples of type names that might require special treatment by some tools. ]

If the current instance represents a generic type, this property applies to the definition of the type. If the current instance represents an unassigned type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsUnicodeClass Property

public virtual bool IsUnicodeClass { get; }

Summary

Indicates whether the type attribute System.Reflection.TypeAttributes.UnicodeClass is selected for the current type.

Property Value

true if the type attribute System.Reflection.TypeAttributes.UnicodeClass is selected for the current type; otherwise, false .

Description

This property is read-only.

If the current Type represents a generic type, this property applies to the definition of the type. If the current Type represents a type parameter of a generic type or method, this property always returns false .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.IsValueType Property

public bool IsValueType { get; }

Summary

Gets a Boolean value that indicates whether the current Type represents a value type.

Property Value

true if the current Type represents a value type (structure); otherwise false .

Description

This property is read-only.

This property returns true for enumerations, but not for the Enum type itself, which is a class. [Note: For an example that demonstrates this behavior, see System.Type.IsEnum .]

See Also

System.Type Class, System Namespace

Type.Module Property

public abstract Module Module { get; }

Summary

Gets the module in which the current Type is defined.

Property Value

A Module that reflects the module in which the current Type is defined.

Description

If the current instance represents a generic type, this property returns the module in which the type was defined.

Similarly, if the current instance represents a generic parameter T, this property returns the assembly that contains the generic type that defines T.

[Behaviors: This property is read-only.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.Namespace Property

public abstract string Namespace { get; }

Summary

Gets the namespace of the Type.

Property Value

A String containing the namespace of the current Type.

Description

If the current instance represents a generic type, this property returns the namespace that contains the generic type definition. Similarly, if the current instance represents a generic parameter T, this property returns the namespace that contains the generic type that defines T.

[Note: A namespace is a logical design-time naming convenience, used mainly to define scope in an application and organize classes and other types in a hierarchical structure. From the viewpoint of the system, there are no namespaces.]

[Behaviors: This property is read-only.]

Library

Reflection

See Also

System.Type Class, System Namespace

Type.ReflectedType Property

public override Type ReflectedType { get; }

Summary

Gets the type that was used to obtain the current instance.

Property Value

The Type object through which the current instance was obtained.

Description

This property is read-only.

If the current instance represents a generic type, or a type parameter of a generic type or method, this property returns the current instance.

Library

Reflection

See Also

System.Type Class, System Namespace

Type.TypeHandle Property

public abstract RuntimeTypeHandle TypeHandle { get; }

Summary

Gets the handle for the current Type.

Property Value

The RuntimeTypeHandle for the current Type.

Description

This property is read-only.

The RuntimeTypeHandle encapsulates a pointer to an internal data structure that represents the type. This handle is unique during the process lifetime. The handle is valid only in the application domain in which it was obtained.

Library

RuntimeInfrastructure

See Also

System.Type Class, System Namespace

Type.TypeInitializer Property

public ConstructorInfo TypeInitializer { get; }

Summary

Gets the initializer for the type represented by the current instance.

Property Value

A ConstructorInfo containing the name of the static constructor for the type represented by the current instance

Description

This property is read-only.

[Note: Type initializers are available through System.Type.GetMember(System.String), System.Type.GetMembers, and System.Type.GetConstructors.]

If the current instance represents an unassigned type parameter of a generic type or method, this property returns null .

Library

Reflection

See Also

System.Type Class, System Namespace

Type.UnderlyingSystemType Property

public abstract Type UnderlyingSystemType { get; }

Summary

Returns the system-supplied type that represents the current type.

Property Value

The underlying system type for the Type.

Description

This property is read-only.

[Behaviors: As described above.]

Library

Reflection

See Also

System.Type Class, System Namespace