public abstract class Type : Object
Object
MemberInfo (excluded)
Type
mscorlib
BCL
Provides information about a type.
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[]).
- Classes
- Value types
- Arrays
- Interfaces
- Pointers
- Enumerations
- Constructed generic types and generic type definitions
- Type arguments and type parameters of constructed generic types, generic type definitions, and generic method definitions
Member Type Static Constructor No Field No Event Not applicable Method No Nested Type No Property Not 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.
System Namespace
Type Constructors
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
protected Type();
Constructs a new instance of the Type class.
Reflection
System.Type Class, System Namespace
public bool Equals(Type o);
Determines if the underlying system type of the current Type is the same as the underlying system type of the specified Type.
- o
- The Type whose underlying system type is to be compared with the underlying system type of the current Type.
true
if the underlying system type of o is the same as the underlying system type of the current Type; otherwise,false
.
Reflection
System.Type Class, System Namespace
public virtual int GetArrayRank();
Returns the number of dimensions in the current Type.
A Int32 containing the number of dimensions in the current Type.
Exception Type Condition ArgumentException The current Type is not an array.
System.Type Class, System Namespace
protected abstract TypeAttributes GetAttributeFlagsImpl();
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.
A TypeAttributes value that signifies the attributes of the type represented by the current instance.
[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.]
Reflection
System.Type Class, System Namespace
public ConstructorInfo GetConstructor(BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers);
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.
- 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
.
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, returnsnull
. 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, returnsnull
.
Exception Type Condition ArgumentNullException types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
The following BindingFlags are used to define which constructors to include in the search:
[Note: For more information, see BindingFlags.]
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public constructors in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public constructors (that is, private and protected constructors) in the search.
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
.
Reflection
System.Type Class, System Namespace
public ConstructorInfo GetConstructor(Type[] types);
Returns a public instance constructor defined in the type represented by the current instance. The parameters of the constructor match the specified argument types.
- 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.
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, returnsnull
.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
.
Exception Type Condition ArgumentNullException types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
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
).
Reflection
System.Type Class, System Namespace
public ConstructorInfo[] GetConstructors();
Returns an array of the public constructors defined in the type represented by the current instance.
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.
This version of System.Type.GetConstructors is equivalent to System.Type.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).
Reflection
System.Type Class, System Namespace
public abstract ConstructorInfo[] GetConstructors(BindingFlags bindingAttr);
Returns an array of constructors defined in the type represented by the current instance, under the specified binding constraints.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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, returnsnull
.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.
The following BindingFlags are used to define which constructors to include in the search:
[Note: For more information, see BindingFlags.]
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public constructors in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public constructors (that is, private and protected constructors) in the search.
[Behaviors: As described above.]
Reflection
System.Type Class, System Namespace
public virtual MemberInfo[] GetDefaultMembers();
Returns an array of MemberInfo objects that reflect the default members defined in the type represented by the current instance.
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.
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.]
Reflection
System.Type Class, System Namespace
public abstract Type GetElementType();
Returns the element type of the current Type .
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, returnsnull
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.
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
System.Type Class, System Namespace
public EventInfo GetEvent(string name);
Returns a EventInfo object reflecting the public event that has the specified name and is defined in the type represented by the current instance.
- name
- A String containing the name of the public event to be returned.
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.
Exception Type Condition ArgumentNullException name is null
.
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.
Reflection
System.Type Class, System Namespace
public abstract EventInfo GetEvent(string name, BindingFlags bindingAttr);
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.
- 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.
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, returnsnull
. If the event is not public, the current instance represents a type from a loaded assembly, and the caller does not have sufficient permission, returnsnull
.If the current instance represents a generic type, this method returns the EventInfo with the type parameters replaced by the appropriate type arguments.
Exception Type Condition ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public events in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public events(that is, private and protected events) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the events declared on the type, not events that were simply inherited.
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.]
Reflection
System.Type Class, System Namespace
public virtual EventInfo[] GetEvents();
Returns an array of EventInfo objects that reflect the public events defined in the type represented by the current instance.
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.
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).]
Reflection
System.Type Class, System Namespace
public abstract EventInfo[] GetEvents(BindingFlags bindingAttr);
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.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns null.
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.
The following BindingFlags are used to define which events to include in the search:
[Note: For more information, see BindingFlags.]
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public events in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public events (that is, private and protected events) in the search.
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.]
Reflection
System.Type Class, System Namespace
public abstract FieldInfo GetField(string name, BindingFlags bindingAttr);
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.
- 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
.
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, returnsnull
. If the field is not public, the current type is from a loaded assembly, and the caller does not have sufficient permission, returnsnull
.If the current instance represents a generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.
Exception Type Condition ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public fields in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public fields (that is, private and protected fields) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the fields declared in the type, not fields that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.]
Reflection
System.Type Class, System Namespace
public FieldInfo GetField(string name);
Returns a FieldInfo object reflecting the field that has the specified name and is defined in the type represented by the current instance.
- name
- A String containing the name of the field to be returned.
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, returnsnull
.If the current instance represents a generic type, this method returns the FieldInfo with the type parameters replaced by the appropriate type arguments.
Exception Type Condition ArgumentNullException name is null
.
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.
Reflection
System.Type Class, System Namespace
public FieldInfo[] GetFields();
Returns an array of FieldInfo objects that reflect the public fields defined in the type represented by the current instance.
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.
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.
Reflection
System.Type Class, System Namespace
public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);
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.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static in order to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public fields in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public fields (that is, private and protected fields) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the fields declared in the type, not fields that were simply inherited.
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.]
Reflection
System.Type Class, System Namespace
public virtual Type[] GetGenericArguments()
Returns an array of Type objects that represent the type arguments of a generic type or the type parameters of a generic type definition.
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.
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.
For an example of using this method, see the example for System.Type.GenericParameterPosition.
Reflection
System.Type Class, System Namespace
public virtual Type[] GetGenericParameterConstraints()
Returns an array of Type objects that represent the type constraints on the current generic type parameter.
An array of Type objects that represent the type constraints on the current generic type parameter.
Exception Type Condition InvalidOperationException The current Type object is not a generic type parameter. That is, the System.Type.IsGenericParameter property returns false
.
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.
Reflection
System.Type Class, System Namespace
public virtual Type GetGenericTypeDefinition()
Returns a Type object that represents a generic type from which the current type can be constructed.
A Type object representing a generic type from which the current type can be constructed.
Exception Type Condition InvalidOperationException The current type is not a generic type. That is, System.Type.HasGenericArguments returns false
.
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.
For an example of using this method, see the example for System.Type.MakeGenericType.
Reflection
System.Type Class, System Namespace
public override int GetHashCode();
Generates a hash code for the current instance.
A Int32 containing the hash code for this instance.
The algorithm used to generate the hash code is unspecified.[Note: This method overrides System.Object.GetHashCode .]
System.Type Class, System Namespace
public abstract Type GetInterface(string name, bool ignoreCase);
Returns the specified interface, specifying whether to do a case-sensitive search.
- 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, andfalse
performs a case-sensitive search.
A Type object representing the interface with the specified name, implemented or inherited by the type represented by the instance, if found; otherwise,null
.
Exception Type Condition ArgumentNullException name is null
.AmbiguousMatchException The current instance represents a type that implements the same generic interface with different type arguments.
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.]
Reflection
System.Type Class, System Namespace
public Type GetInterface(string name);
Searches for the interface with the specified name.
- name
- A String containing the name of the interface to get.
A Type object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise,null
.
Exception Type Condition ArgumentNullException name is null
.AmbiguousMatchException The current instance represents a type that implements the same generic interface with different type arguments.
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.]
Reflection
System.Type Class, System Namespace
public abstract Type[] GetInterfaces();
Returns all interfaces implemented or inherited by the type represented by the current instance.
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.
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.]
Reflection
System.Type Class, System Namespace
public virtual MemberInfo[] GetMember(string name, BindingFlags bindingAttr);
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.
- 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
.
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, returnsnull
.
Exception Type Condition ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.]
Reflection
System.Type Class, System Namespace
public MemberInfo[] GetMember(string name);
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.
- name
- A String containing the name of the members to be returned.
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.
Exception Type Condition ArgumentNullException name is null
.
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.]
Reflection
System.Type Class, System Namespace
public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr);
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.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
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.]
Reflection
System.Type Class, System Namespace
public MemberInfo[] GetMembers();
Returns an array of MemberInfo objects that reflect the public members defined in the type represented by the current instance.
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.
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.
Reflection
System.Type Class, System Namespace
public MethodInfo GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers);
Returns a MethodInfo object that reflects the method that matches the specified criteria and is defined in the type represented by the current instance.
- 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
.
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, returnsnull
. 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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one method matching the specified criteria was found. ArgumentNullException name or types is null
.-or-
At least one of the elements in types is
null
.
ArgumentException types has more than one dimension.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.]
Reflection
System.Type Class, System Namespace
public MethodInfo GetMethod(string name, BindingFlags bindingAttr);
Returns a MethodInfo object that reflects the method that has the specified name and is defined in the type represented by the current instance.
- 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
.
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
.
Exception Type Condition AmbiguousMatchException More than one method matching the specified criteria was found. ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.]
Reflection
System.Type Class, System Namespace
public MethodInfo GetMethod(string name);
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.
- name
- A String containing the name of the public method to be returned.
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
.
Exception Type Condition AmbiguousMatchException More than one method matching the specified criteria was found. ArgumentNullException name is null
.
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.]
Reflection
System.Type Class, System Namespace
public MethodInfo GetMethod(string name, Type[] types);
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.
- 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.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one method matching the specified criteria was found. ArgumentNullException name or types is null
.-or-
At least one of the elements in types is
null
.
ArgumentException types has more than one dimension.
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.]
Reflection
System.Type Class, System Namespace
public MethodInfo GetMethod(string name, Type[] types, ParameterModifier[] modifiers);
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.
- 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
.
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
.
Exception Type Condition AmbiguousMatchException More than one method matching the specified criteria was found. ArgumentNullException name or types is null
.-or-
At least one of the elements in types is
null
.
ArgumentException types has more than one dimension.
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.]
Reflection
System.Type Class, System Namespace
public abstract MethodInfo[] GetMethods(BindingFlags bindingAttr);
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.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
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.]
Reflection
System.Type Class, System Namespace
public MethodInfo[] GetMethods();
Returns the public methods defined in the type represented by the current instance.
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.
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.
Reflection
System.Type Class, System Namespace
public Type GetNestedType(string name);
Returns the public nested type defined in the type represented by the current instance
- 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"). ]
A Type object representing the public nested type with the specified name, if found; otherwise,null
.
Exception Type Condition ArgumentNullException name is null
.
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.
Reflection
System.Type Class, System Namespace
public abstract Type GetNestedType(string name, BindingFlags bindingAttr);
Returns a nested types defined in the type represented by the current instance that match the specified binding constraints.
- 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
.
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, returnsnull
.
Exception Type Condition ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.]
Reflection
System.Type Class, System Namespace
public Type[] GetNestedTypes();
Returns all the public types nested within the current Type.
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.
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.
Reflection
System.Type Class, System Namespace
public abstract Type[] GetNestedTypes(BindingFlags bindingAttr);
Returns an array containing the nested types defined in the type represented by the current instance that match the specified binding constraints.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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.
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:
- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
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.
Reflection
System.Type Class, System Namespace
public abstract PropertyInfo[] GetProperties(BindingFlags bindingAttr);
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.
- bindingAttr
- A bitwise combination of BindingFlags values that control the binding process. If zero is specified, this method returns
null
.
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.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
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 ispublic
. Otherwise, the property is notpublic
.]
Reflection
System.Type Class, System Namespace
public PropertyInfo[] GetProperties();
Returns an array of PropertyInfo objects that reflect the public properties defined in the type represented by the current instance.
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.
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 ispublic
. Otherwise, the property is considered to be notpublic
.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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name, Type returnType, Type[] types);
Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.
- 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.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name or types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name, Type[] types);
Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.
- 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.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name or types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name, Type returnType);
Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that matches the specified search criteria.
- 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.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name is null
.
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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name);
Returns a PropertyInfo object that reflects the public property defined in the type represented by the current instance that has the specified name.
- name
- A String containing the name of the property to be returned.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name is null
.
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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr);
Returns a PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria.
- 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
.
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, returnsnull
. 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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name is null
.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.
Reflection
System.Type Class, System Namespace
public PropertyInfo GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
Returns a PropertyInfo object that reflects the property defined in the type represented by the current instance that matches the specified search criteria .
- 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
.
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, returnsnull
. 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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name or types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
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.
Reflection
System.Type Class, System Namespace
protected abstract PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers);
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.
- 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
.
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, returnsnull
.
Exception Type Condition AmbiguousMatchException More than one property matching the specified criteria was found. ArgumentNullException name or types is null
, or at least one of the elements in types isnull
.
ArgumentException types has more than one dimension.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
[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. ]
Reflection
System.Type Class, System Namespace
public static Type GetType(string typeName, bool throwOnError, bool ignoreCase);
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.
- 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. Specifyfalse
to ignore errors while loading the Type.
- ignoreCase
- A Boolean. Specify
true
to perform a case-insensitive search for typeName. Specifyfalse
to perform a case-sensitive search for typeName .
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 returnsnull
.
Exception Type Condition ArgumentNullException typeName is null
.TargetInvocationException A type initializer was invoked and threw an exception. TypeLoadException throwOnError is true
and an error was encountered while loading the selected Type.
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 Type Use 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 class Type.GetType("MyParentClass+MyNestedClass")
A one-dimensional array with a lower bound of 0 Type.GetType("MyArray[]")
A one-dimensional array with an unknown lower bound Type.GetType("MyArray[*]")
An n-dimensional array A comma (,) inside the brackets a total of n-1 times. For example, System.Object[,,]
represents a three-dimensionalObject
array.A two-dimensional array's array Type.GetType("MyArray[][]")
A rectangular two-dimensional array with unknown lower bounds Type.GetType("MyArray[*,*]")
orType.GetType("MyArray[,]")
A generic type with one type argument Type.GetType("MyGenericType[MyType]")
A generic type with two type arguments Type.GetType("MyGenericType[MyType,AnotherType]")
A generic type with two assembly-qualified type arguments Type.GetType("MyGenericType[[MyType,MyAssembly], [AnotherType,AnotherAssembly]]")
An assembly-qualified generic type with an assembly-qualified type argument Type.GetType("MyGenericType[[MyType,MyAssembly]] ,MyGenericTypeAssembly")
A generic type whose type argument is a generic type with two type arguments Type.GetType("MyGenericType[AnotherGenericType [MyType,AnotherType]]")
Reflection
System.Type Class, System Namespace
public static Type GetType(string typeName, bool throwOnError);
Returns the Type with the specified name, optionally throwing an exception if an error occurs while loading the Type .
- 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. Specifyfalse
to ignore errors while loading the Type.
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 returnsnull
.
Exception Type Condition ArgumentNullException typeName is null
.TargetInvocationException A type initializer was invoked and threw an exception. TypeLoadException throwOnError is true
and an error was encountered while loading the Type.
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). ]
Reflection
System.Type Class, System Namespace
public static Type GetType(string typeName);
Returns the Type with the specified name.
- typeName
- A String containing the case-sensitive name of the Type to return.
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 returnsnull
.
Exception Type Condition ArgumentNullException typeName is null
.TargetInvocationException A type initializer was invoked and threw an exception.
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).]
Reflection
System.Type Class, System Namespace
public static Type[] GetTypeArray(object[] args);
Returns the types of the objects in the specified array.
- args
- An array of objects whose types are to be returned.
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 benull
.
Exception Type Condition ArgumentNullException args is null
.TargetInvocationException The type initializers were invoked and at least one threw an exception.
Reflection
System.Type Class, System Namespace
public static Type GetTypeFromHandle(RuntimeTypeHandle handle);
Gets the Type referenced by the specified type handle.
- handle
- The RuntimeTypeHandle object that refers to the desired Type.
The Type referenced by the specified RuntimeTypeHandle.
Exception Type Condition ArgumentNullException handle is null
.SecurityException The requested type is non-public and outside the current assembly, and the caller does not have the required permission. TargetInvocationException A type initializer was invoked and threw an exception.
The handles are valid only in the application domain in which they were obtained.
RuntimeInfrastructure
System.Type Class, System Namespace
public static RuntimeTypeHandle GetTypeHandle(object o);
Returns the handle for the Type of the specified object.
- o
- The object for which to get the type handle.
The RuntimeTypeHandle for the Type of the specified Object.
The handle is valid only in the application domain in which it was obtained.
RuntimeInfrastructure
System.Type Class, System Namespace
protected abstract bool HasElementTypeImpl();
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.
true
if the Type is an array, a pointer, or is passed by reference; otherwise,false
.
[Note: For example, System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32[]").HasElementTypeImpl returnstrue
, but System.Type.GetType(System.String,System.Boolean,System.Boolean) ("System.Int32").HasElementTypeImpl returnsfalse
. System.Type.HasElementTypeImpl also returnstrue
for "System.Int32*" and "System.Int32&".]
Reflection
System.Type Class, System Namespace
public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, CultureInfo culture);
Invokes the specified member, using the specified binding constraints and matching the specified argument list and culture.
- 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
.
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 .
Exception Type Condition ArgumentNullException name is null
.
ArgumentException args 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.
MissingFieldException A field or property matching the specified criteria was not found. MissingMethodException A 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
).
MethodAccessException The requested member is non-public and the caller does not have the required permission. TargetException The member matching the specified criteria cannot be invoked on target. TargetInvocationException The member matching the specified criteria threw an exception. AmbiguousMatchException More than one member matches the specified criteria.
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
).
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[]).
Reflection
System.Type Class, System Namespace
public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);
Invokes the specified member, using the specified binding constraints and matching the specified argument list.
- 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.
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 .
Exception Type Condition ArgumentNullException name is null
.
ArgumentException args 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.
MissingFieldException A field or property matching the specified criteria was not found. MissingMethodException A 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
).
MethodAccessException The requested member is non-public and the caller does not have the required permission. TargetException The member matching the specified criteria cannot be invoked on target. TargetInvocationException The member matching the specified criteria threw an exception. AmbiguousMatchException More than one member matches the specified criteria.
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[]).]
Reflection
System.Type Class, System Namespace
public abstract object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters);
Invokes or accesses a member defined on the type represented by the current instance that matches the specified binding criteria.
- 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 notnull
, the order of the elements in args corresponds to the order of the parameters specified in namedParameters. Specify an empty array ornull
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. Specifynull
to have this parameter ignored.
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 .
Exception Type Condition ArgumentNullException name is null
.
ArgumentException args 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.
MissingFieldException A field or property matching the specified criteria was not found. MissingMethodException A 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
).
MethodAccessException The requested member is non-public and the caller does not have the required permission. TargetException The member matching the specified criteria cannot be invoked on target. TargetInvocationException The member matching the specified criteria threw an exception. AmbiguousMatchException More than one member matches the specified criteria.
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:
- Specify either System.Reflection.BindingFlags.Instance or System.Reflection.BindingFlags.Static to get a return value other than
null
.- Specify System.Reflection.BindingFlags.Public to include public members in the search.
- Specify System.Reflection.BindingFlags.NonPublic to include non-public members (that is, private and protected members) in the search.
[Note: For more information, see BindingFlags.]
- System.Reflection.BindingFlags.DeclaredOnly to search only the members declared in the type, not members that were simply inherited.
- System.Reflection.BindingFlags.IgnoreCase to ignore the case of name.
[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:
Action BindingFlags 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".
]
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 "".
Reflection
System.Type Class, System Namespace
protected abstract bool IsArrayImpl();
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.
true
if the Type is an array; otherwise,false
.
An instance of the Array class is required to returnfalse
because it is an object, not an array.[Behaviors: As described above.]
Reflection
System.Type Class, System Namespace
public virtual bool IsAssignableFrom(Type c);
Determines whether an instance of the current Type can be assigned from an instance of the specified Type .
- c
- The Type to compare with the current Type .
false
if c is a null reference.
true
if one or more of the following statements are true; otherwisefalse
.
- If c and the current Type represent the same type.
- If the current Type is in the inheritance hierarchy of c.
- If the current Type is an interface and c supports that interface.
- If c is a generic type parameter and the current instance represents one of the constraints of c.
[Note: A generic type definition is not assignable from a closed constructed type.]
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
System.Type Class, System Namespace
protected abstract bool IsByRefImpl();
When overridden in a derived class, implements the System.Type.IsByRef property and determines whether the Type is passed by reference.
true
if the Type is passed by reference; otherwise,false
.
[Behaviors: As described above.]
Reflection
System.Type Class, System Namespace
protected abstract bool IsCOMObjectImpl();
Reserved.
false
This abstract method is required to be present for legacy implementations. Conforming implementations are permitted to throw the NotSupportedException as their implementation.
Reflection
System.Type Class, System Namespace
public virtual bool IsInstanceOfType(object o);
Determines whether the specified object is an instance of the current Type.
- o
- The object to compare with the current Type.
true
if either of the following statements is true; otherwisefalse
.
If o is a null reference or if the current instance is an open generic type (that is, System.Type.ContainsGenericParameters returns
- If the current Type is in the inheritance hierarchy of o.
- If the current Type is an interface and o supports that interface.
true
) returnsfalse
.
As described above.[Note: A constructed type is not an instance of its generic type definition.
]
[Behaviors: As described above.]
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
System.Type Class, System Namespace
protected abstract bool IsPointerImpl();
When overridden in a derived class, implements the System.Type.IsPointer property and determines whether the Type is a pointer.
true
if the Type is a pointer; otherwise,false
.
[Behaviors: As described above.]
Reflection
System.Type Class, System Namespace
protected abstract bool IsPrimitiveImpl();
When overridden in a derived class, implements the System.Type.IsPrimitive property and determines whether the Type is one of the primitive types.
true
if the Type is one of the primitive types; otherwise,false
.
[Behaviors: This method returnstrue
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.]
Reflection
System.Type Class, System Namespace
public virtual bool IsSubclassOf(Type c);
Determines whether the current Type derives from the specified Type .
- c
- The Type to compare with the current Type.
true
if c and the current Type represent classes, and the class represented by the current Type derives from the class represented by c; otherwisefalse
. Returnsfalse
if c and the current Type represent the same class.
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.
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
System.Type Class, System Namespace
public virtual Type MakeArrayType()
Returns a Type object representing a one-dimensional array type whose element type is the current type, with a lower bound of zero.
A Type object representing a one-dimensional array type whose element type is the current type, with a lower bound of zero.
This method provides a way to generate an array type with any possible element type, including generic types.
Reflection
System.Type Class, System Namespace
public virtual Type MakeArrayType(int rank)
Returns a Type object representing an array of the current type, with the specified number of dimensions.
- rank
- The number of dimensions for the array.
A Type object representing an array of the current type, with the specified number of dimensions.
Exception Type Condition IndexOutOfRangeException rank is invalid (being less than 1, for example).
This method provides a way to generate an array with any possible element type, including generic types.
Reflection
System.Type Class, System Namespace
public virtual Type MakeByRefType()
Returns a Type object that represents the current type when passed as a byref parameter.
A Type object that represents the current type when passed as a byref parameter.
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 representingint32&
.]
Reflection
System.Type Class, System Namespace
public override Type MakeGenericType(System.Type[] typeArguments)
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.
- typeArguments
- An array of types to be substituted for the type parameters of the current generic type definition.
A Type representing the constructed type formed by substituting the elements of typeArguments for the type parameters of the current generic type definition.
Exception Type Condition ArgumentException The 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.
ArgumentNullException typeArguments is null
.-or-
An element of typeArguments is
null
.
InvalidOperationException The current type does not represent the definition of a generic type. That is, System.Type.IsGenericTypeDefinition returns false
.
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.
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 ofTest
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 */
Reflection
System.Type Class, System Namespace
public virtual Type MakePointerType()
Returns a Type object that represents the type of an unmanaged pointer to the current type.
A Type object that represents the type of an unmanaged pointer to the current type.
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 representingint32*
.]
Reflection
System.Type Class, System Namespace
public override string ToString();
Returns a String representation of the current Type.
Returns System.Type.FullName .
[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.
System.Type Class, System Namespace
public static readonly char Delimiter;
Specifies the character that separates elements in the fully qualified name of a Type .
This field is read-only.
Reflection
System.Type Class, System Namespace
public static readonly Type[] EmptyTypes;
Returns an empty array of type Type .
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
).]
Reflection
System.Type Class, System Namespace
public static readonly object Missing;
Represents a missing value in the Type information.
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 theMissing
field is specified for a parameter value and there is no default value for that parameter, a ArgumentException is thrown.]
Reflection
System.Type Class, System Namespace
public abstract Assembly Assembly { get; }
Gets the Assembly in which the type is declared. For generic types, gets the Assembly that contains the generic type definition.
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.
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 namedMyGenericStack<T>
. If you create an instance ofMyGenericStack<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.]
RuntimeInfrastructure
System.Type Class, System Namespace
public abstract string AssemblyQualifiedName { get; }
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.
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 returnsnull
.
[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.
[Note: For example, the fully qualified name for a class might look like this:
Delimiter Meaning 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.
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.
]
Reflection
System.Type Class, System Namespace
public TypeAttributes Attributes { get; }
Gets the attributes associated with the type represented by the current instance.
A TypeAttributes object representing the attribute set of the Type.
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.
Reflection
System.Type Class, System Namespace
public abstract Type BaseType { get; }
Gets the base Type of the current Type .
A Type object representing the type from which the current Type directly inherits, ornull
if the current Type represents the Object class.
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, thereforenull
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.]
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
System.Type Class, System Namespace
public virtual bool ContainsGenericParameters { get; }
Gets a value that indicates whether a Type object contains unassigned generic parameters.
true
if a Type object contains unassigned generic parameters; otherwisefalse
.
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 returnstrue
, 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 returnsfalse
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.
]
For an example of using this method, see the example for System.Type.GenericParameterPosition.
Reflection
System.Type Class, System Namespace
public virtual MethodBase DeclaringMethod { get; }
If the current Type represents a type parameter of a generic method, gets a MethodInfo that represents the declaring method.
If the current Type represents a type parameter of a generic method, a MethodInfo that represents the declaring method; otherwisenull
.
The declaring method is a generic method definition. That is, if System.Type.DeclaringMethod does not returnnull
, then DeclaringMethod.IsGenericMethodDefinition returnstrue
.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.
- If the System.Type.DeclaringMethod property returns a MethodBase, that MethodBase represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.
- If the System.Type.DeclaringMethod property returns a
null
, then the System.Type.DeclaringType property always returns a Type object representing a generic type definition, and the current Type object represents a type parameter of that generic type definition.
[Behaviors: This property is read-only.
]
Reflection
System.Type Class, System Namespace
public virtual Type DeclaringType { get; }
Gets the type that declares the type represented by the current instance.
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.
[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.
- If the System.Type.DeclaringMethod property returns a MethodBase, that MethodBase represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.
- If the System.Type.DeclaringMethod property returns a
null
, then the System.Type.DeclaringType property always returns a Type object representing a generic type definition, and the current Type object represents a type parameter of that generic type definition.
Reflection
System.Type Class, System Namespace
public static Binder DefaultBinder { get; }
Gets the default binder used by the system.
The default Binder used by the system.
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 Type Target Type Any type Its base type. Any type The interface it implements. Char Unt16, UInt32, Int32, UInt64, Int64, Single, Double Byte Char, Unt16, Int16, UInt32, Int32, UInt64, Int64, Single, Double SByte Int16, Int32, Int64, Single, Double UInt16 UInt32, Int32, UInt64, Int64, Single, Double Int16 Int32, Int64, Single, Double UInt32 UInt64, Int64, Single, Double Int32 Int64, Single, Double UInt64 Single, Double Int64 Single, Double Single Double Non-reference By-reference.
Reflection
System.Type Class, System Namespace
public abstract string FullName { get; }
Gets the fully qualified name of the type represented by the current instance.
A String containing the fully qualified name of the Type.
[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 ast.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.]
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
System.Type Class, System Namespace
public virtual GenericParameterAttributes GenericParameterAttributes { get; }
Gets a combination of GenericParameterAttributes flags that describe the variance and special constraints of the current generic type parameter.
A GenericParameterAttributes value that describes the variance and special constraints of the current generic type parameter.
Exception Type Condition InvalidOperationException The current Type object is not a generic type parameter. That is, the System.Type.IsGenericParameter property returns false
.
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.
Reflection
System.Type Class, System Namespace
public virtual int GenericParameterPosition { get; }
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.
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.
Exception Type Condition InvalidOperationException The current type does not represent a type parameter. That is, System.Type.IsGenericParameter returns false
.
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
- If the System.Type.DeclaringMethod property returns a MethodBase, that MethodBase represents a generic method definition, and the current Type object represents a type parameter of that generic method definition.
- If the System.Type.DeclaringMethod property returns a
null
, then the System.Type.DeclaringType property always returns a Type object representing a generic type definition, and the current Type object represents a type parameter of that generic type definition.
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 byGetSomething
depends on the type arguments supplied to classA
andGetSomething
itself. You can obtain a MethodInfo forGetSomething
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 ofV
is zero becauseV
is the first type parameter in the type parameter list for classA
. The position ofX
is zero becauseX
is the first type parameter in the type parameter list forGetSomething
.[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 returns
true
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.]
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) */
Reflection
System.Type Class, System Namespace
public bool HasElementType { get; }
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.
true
if the Type is an array, a pointer, or is passed by reference; otherwise,false
.
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 returnsfalse
. System.Type.HasElementType also returnstrue
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
.
Reflection
System.Type Class, System Namespace
public bool IsAbstract { get; }
Gets a Boolean value indicating whether the type represented by the current instance is abstract and is required to be overridden.
true
if the Type is abstract; otherwise,false
.
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.
Reflection
System.Type Class, System Namespace
public virtual bool IsAnsiClass { get; }
Indicates whether the type attribute System.Reflection.TypeAttributes.AnsiClass is selected for the current type.
true
if the type attribute System.Reflection.TypeAttributes.AnsiClass is selected for the current type; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsArray { get; }
Gets a Boolean value that indicates whether the current Type represents an array.
true
if the current Type represents an array; otherwisefalse
.
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
.
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
System.Type Class, System Namespace
public virtual bool IsAutoClass { get; }
Indicates whether the type attribute System.Reflection.TypeAttributes.AutoClass is selected for the current type.
true
if the type attribute System.Reflection.TypeAttributes.AutoClass is selected for the current type; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsAutoLayout { get; }
Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.AutoLayout is specified for the Type.
true
if the type layout attribute System.Reflection.TypeAttributes.AutoLayout is specified for the current Type; otherwise,false
.
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.
]
Reflection
System.Type Class, System Namespace
public bool IsByRef { get; }
Gets a Boolean value indicating whether the Type is passed by reference.
true
if the Type is passed by reference; otherwise,false
.
This property is read-only.
Reflection
System.Type Class, System Namespace
public bool IsClass { get; }
Gets a Boolean value that indicates whether the current Type represents a class.
true
if the current Type represents a class; otherwisefalse
.
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
.
System.Type Class, System Namespace
public bool IsEnum { get; }
Gets a Boolean value that indicates whether the current Type represents an enumeration.
true
if the current Type represents an enumeration; otherwisefalse
.
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
.
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
System.Type Class, System Namespace
public bool IsExplicitLayout { get; }
Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.ExplicitLayout is specified for the Type.
true
if the type layout attribute System.Reflection.TypeAttributes.ExplicitLayout is specified for the current Type; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public virtual bool IsGenericParameter { get; }
Gets a value that indicates whether the current type represents a type parameter of a generic type or method.
true
if the current object represents a type parameter of a generic type or method; otherwisefalse
.
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 andtrue
for the type parameters.For a list of the invariant conditions for terms used in generic reflection, see the System.Type.IsGenericType property description.
For an example of using this method, see the example for System.Type.GenericParameterPosition.
Reflection
System.Type Class, System Namespace
public virtual bool IsGenericType { get; }
Gets a value that indicates whether the current type has type arguments, and is therefore a generic type.
true
if the current type has type arguments; otherwisefalse
.
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.
Term Invariant generic type definition The 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 type The 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 type The 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 type The 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 parameter The System.Type.IsGenericParameter property is true
.In a generic type definition, a placeholder for a type that will be assigned later.
generic type argument Can 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 befalse
for all the type arguments.
[Behaviors: This property is read-only.
]
For an example of using this method, see the example for System.Type.MakeGenericType.
Reflection
System.Type Class, System Namespace
public virtual bool IsGenericTypeDefinition { get; }
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.
true
if the current object represents the definition of a generic type, none of whose type parameters have been bound to specific types; otherwisefalse
.
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.
]
For an example of using this method, see the example for System.Type.MakeGenericType..
Reflection
System.Type Class, System Namespace
public bool IsImport { get; }
Gets a Boolean value indicating whether the Type was imported from another class.
true
if the Type was imported from another class; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsInterface { get; }
Gets a Boolean value that indicates whether the current Type represents an interface.
true
if the current Type represents an interface; otherwisefalse
.
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
.
System.Type Class, System Namespace
public bool IsLayoutSequential { get; }
Gets a Boolean value indicating whether the type layout attribute System.Reflection.TypeAttributes.SequentialLayout is specified for the Type.
true
if the type layout attribute System.Reflection.TypeAttributes.SequentialLayout is specified for the current Type; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsMarshalByRef { get; }
Gets a Boolean value indicating whether the current type is marshaled by reference.
true
if the Type is marshaled by reference; otherwise,false
.
This property is read-only.
Reflection
System.Type Class, System Namespace
public bool IsNestedAssembly { get; }
Gets a Boolean value indicating whether the current Type is nested and visible only within its own assembly.
true
if the Type is nested and visible only within its own assembly; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
false
.
Reflection
System.Type Class, System Namespace
public bool IsNestedFamANDAssem { get; }
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.
true
if the Type is nested and visible only to classes that belong to both its own family and its own assembly; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsNestedFamORAssem { get; }
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.
true
if the Type is nested and visible only to classes that belong to its own family or to its own assembly; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsNestedFamily { get; }
Gets a Boolean value indicating whether the current Type is nested and visible only within its own family.
true
if the Type is nested and visible only within its own family; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsNestedPrivate { get; }
Gets a Boolean value indicating whether the current Type is nested and declared private.
true
if the Type is nested and declared private; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
false
.
Reflection
System.Type Class, System Namespace
public bool IsNestedPublic { get; }
Gets a Boolean value indicating whether the current Type is a public nested class.
true
if the class is nested and declared public; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
false
.
Reflection
System.Type Class, System Namespace
public bool IsNotPublic { get; }
Gets a Boolean value indicating whether the top-level Type is not declared public.
true
if the top-level Type is not declared public; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
false
.
Reflection
System.Type Class, System Namespace
public bool IsPointer { get; }
Gets a Boolean value that indicates whether the current Type represents a pointer.
This property is read-only.
true
if the current Type represents a pointer; otherwisefalse
.
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
.
System.Type Class, System Namespace
public bool IsPrimitive { get; }
Gets a Boolean value indicating whether the current Type is one of the primitive types.
true
if the Type is one of the primitive types; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsPublic { get; }
Gets a Boolean value indicating whether the top-level Type is declared public.
true
if the top-level Type is declared public; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
true
.
Reflection
System.Type Class, System Namespace
public bool IsSealed { get; }
Gets a Boolean value indicating whether the current Type is declared sealed.
true
if the Type is declared sealed; otherwise,false
.
This property is read-only.If the current instance represents an unassigned type parameter of a generic type, this property returns
true
.
Reflection
System.Type Class, System Namespace
public bool IsSpecialName { get; }
Gets a Boolean value indicating whether the current Type has a name that requires special handling.
true
if the Type has a name that requires special handling; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public virtual bool IsUnicodeClass { get; }
Indicates whether the type attribute System.Reflection.TypeAttributes.UnicodeClass is selected for the current type.
true
if the type attribute System.Reflection.TypeAttributes.UnicodeClass is selected for the current type; otherwise,false
.
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
.
Reflection
System.Type Class, System Namespace
public bool IsValueType { get; }
Gets a Boolean value that indicates whether the current Type represents a value type.
true
if the current Type represents a value type (structure); otherwisefalse
.
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 .]
System.Type Class, System Namespace
public abstract Module Module { get; }
Gets the module in which the current Type is defined.
A Module that reflects the module in which the current Type is defined.
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.]
Reflection
System.Type Class, System Namespace
public abstract string Namespace { get; }
Gets the namespace of the Type.
A String containing the namespace of the current Type.
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.]
Reflection
System.Type Class, System Namespace
public override Type ReflectedType { get; }
Gets the type that was used to obtain the current instance.
TheType
object through which the current instance was obtained.
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.
Reflection
System.Type Class, System Namespace
public abstract RuntimeTypeHandle TypeHandle { get; }
Gets the handle for the current Type.
The RuntimeTypeHandle for the current Type.
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.
RuntimeInfrastructure
System.Type Class, System Namespace
public ConstructorInfo TypeInitializer { get; }
Gets the initializer for thetype represented by the current instance.
A ConstructorInfo containing the name of the static constructor for the type represented by the current instance
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
.
Reflection
System.Type Class, System Namespace
public abstract Type UnderlyingSystemType { get; }
Returns the system-supplied type that represents the current type.
The underlying system type for the Type.
This property is read-only.[Behaviors: As described above.]
Reflection
System.Type Class, System Namespace