A class’s type provides properties and methods that provide a wealth of information about the class. You can get a Type
object representing a class by using typeof(TheClass)
or by calling the GetType
method on an instance of the class.
The following section summarizes the Type
class’s most useful properties and methods. The sections after that summarize other useful reflection classes.
The following table lists the Type
class’s most useful properties.
Property | Description |
Assembly | Gets the assembly where the type is declared. |
Attributes | Gets the type’s attributes. |
BaseType | Gets the type’s parent class. |
ContainsGenericParameters | Indicates whether the type has generic type parameters. |
CustomAttributes | Gets the type’s custom attributes. |
FullName | Gets the type’s fully qualified name, which includes the type’s namespace but not its assembly. |
IsAbstract | Returns true if the type is abstract. |
IsArray | Returns true if the type is an array. |
IsAutoLayout | Returns true if the type’s fields are laid out automatically by the CLR. |
IsByRef | Returns true if the type is passed by reference. |
IsClass | Returns true if the type is a class. |
IsCOMObject | Returns true if the type is a COM object. |
IsEnum | Returns true if the type is an enumeration. |
IsExplicitLayout | Returns true if the type’s fields are laid out explicitly. |
IsGenericType | Gets a value indicating whether the current type is a generic type. |
IsImport | Returns true if the type was imported from a COM library. |
IsInterface | Returns true if the type is an interface. |
IsLayoutSequential | Returns true if the type’s fields are laid out sequentially. |
IsMarshalByRef | Returns true if the type is marshaled by reference. |
IsNested | Returns true if the type’s definition is contained inside another type’s definition. |
IsNestedAssembly | Returns true if the type is nested and visible only within its assembly. |
IsNestedFamANDAssem | Returns true if the type is nested and visible only to classes in its family in its class and derived classes) and its assembly. |
IsNestedFamily | Returns true if the type is nested and visible only to classes in its family. |
IsNestedFamORAssem | Returns true if the type is nested and visible only to classes either in its family or in its assembly. |
IsNestedPrivate | Returns true if the type is nested and private. |
IsNestedPublic | Returns true if the type is nested and public. |
IsNotPublic | Returns true if the type is not public. |
IsPointer | Returns true if the type is a pointer. |
IsPrimitive | Returns true if the type is a primitive type. |
IsPublic | Returns true if the type is public. |
IsSealed | Returns true if the type is sealed. |
IsSerializable | Returns true if the type is serializable. |
IsValueType | Returns true if the type is a value type. |
IsVisible | Returns true if the type is visible to code outside the assembly. |
Module | Returns the module where the type is defined. |
Name | Returns the type’s name. |
Namespace | Returns the type’s namespace. |
StructLayoutAttribute | Returns a StructLayoutAttribute describing the type’s layout. |
TypeInitializer | Gets the type’s initializer. |
The following table lists the Type
class’s most useful methods.
Method | Description |
FindInterfaces | Returns a filtered list of interfaces that the type implements. |
FindMembers | Returns a filtered array of MemberInfo objects of the specified member type. Member type can be All , Constructor , Custom , Event , Field , Method , NestedType , Property , or TypeInfo . |
GetArrayRank | Returns the number of an array’s dimensions. |
GetConstructor | Returns information about a constructor matching specific parameter types. |
GetConstructors | Returns all the type’s public constructors. |
GetCustomAttributes | Returns an array of the type’s custom attributes. |
GetDefaultMembers | Returns the type’s default members. |
GetElementType | For an array, pointer, or reference type, returns the type of items contained by the type. |
GetEnumName | For an enumeration type, returns the name corresponding to a specific value. (For example, if the Desserts enumeration represents the value Cookie with the integer value 3, then GetEnumName(3) returns Cookie .) |
GetEnumNames | For an enumeration type, returns the names of the enumeration’s values. |
GetEnumUnderlyingType | For an enumeration type, returns the integer data type used to store the enumeration’s values. |
GetEnumValues | For an enumeration type, returns an array of the integer values used to store the enumeration’s values. |
GetEvent | Returns an EventInfo object representing a specified public event. |
GetEvents | Returns EventInfo objects representing all the type’s public events. |
GetField | Returns a FieldInfo object representing a specified public field. |
GetFields | Returns FieldInfo objects representing all the type’s public fields. |
GetGenericArguments | Returns an array of type objects representing the type’s generic type arguments. |
GetGenericParameterConstraints | Returns an array of type objects representing constraints on a generic type parameter. |
GetInterface | Returns a type representing an interface implemented by the type. |
GetInterfaces | Returns an array of types representing interfaces implemented by the type. |
GetMember | Returns an array of MemberInfo objects containing information about the type’s public members that match search criteria. |
GetMembers | Returns an array of MemberInfo objects containing information about all the type’s public members. |
GetMethod | Returns a MethodInfo object representing a public method that matches search criteria. |
GetMethods | Returns an array of MethodInfo objects representing the type’s public methods. |
GetNestedType | Returns type information representing a nested type that matches search criteria. |
GetNestedTypes | Returns an array of type information representing all the type’s nested types. |
GetProperties | Returns an array of PropertyInfo objects representing properties that match search criteria. |
GetProperty | Returns a PropertyInfo object representing a property that matches search criteria. |
GetType | This static method returns a Type object representing a type that matches search criteria. |
GetTypeArray | Returns an array of Type objects for objects in an array. |
GetTypeFromCLSID | Returns the type with a given class identifier (CLSID) specified as a GUID. |
InvokeMember | Invokes a member. |
IsAssignableFrom | Returns true if an instance of the type can be assigned from an instance of another specified type. |
IsEnumDefined | For an enumeration type, returns true if the enumeration defines a specific value. |
IsInstanceOfType | Returns true if a specific object is an instance of the type. |
IsSubclassOf | Returns true if the type is a subclass of another type. |
MakeArrayType | Returns a new Type object representing an array of the current type. |
MakeByRefType | Returns a new Type object representing the current type when passed by reference. |
MakeGenericType | Returns a new Type object representing the current generic type with specific types substituted in for the generic type parameters. |
MakePointerType | Returns a new Type object representing a pointer to the current type. |
The MemberInfo
class is the parent class of EventInfo
, MethodInfo
, FieldInfo
, and PropertyInfo
. The following table lists useful MemberInfo
properties.
Property | Description |
CustomAttributes | Returns a collection containing information about the member’s custom attributes |
DeclaringType | Returns the class that includes this member |
MemberType | Returns the member’s type |
Module | Returns the module that contains the type that defines this member |
Name | Returns the member’s name |
ReflectedType | Returns the object that was used to get this MemberInfo object |
The following sections describe the most useful properties and methods provided by the EventInfo
, MethodInfo
, FieldInfo
, and PropertyInfo
classes.
An EventInfo
object contains information about an event. The following table lists the most useful EventInfo
properties that are not inherited from the MemberInfo
class.
Property | Description |
AddMethod | Returns the method used to add a delegate to the event |
Attributes | Returns the event’s attributes |
EventHandlerType | Returns a Type representing the event’s event handler delegate |
RaiseMethod | Returns the method that is called when the event is raised |
RemoveMethod | Returns the method that removes a delegate from the event handler |
The following table lists the most useful EventInfo
methods that are not inherited from the MemberInfo
class.
Method | Description |
AddEventHandler | Adds an event handler to the event |
GetAddMethod | Returns the method used to add a delegate to the event |
GetRaiseMethod | Returns the method that is called when the event is raised |
GetRemoveMethod | Returns the method that removes a delegate from the event handler |
RemoveEventHandler | Removes an event handler from the event |
A MethodInfo
object contains information about a method. This class is derived from MethodBase
, which is derived from MemberInfo
.
The following table lists the most useful MethodInfo
properties that are not inherited from the MemberInfo
class. (Some of these properties are inherited from MethodBase
, but that class is abstract, so this appendix doesn’t describe it separately.)
Property | Description |
ContainsGenericParameters | Returns true if the method has generic parameters. |
IsAbstract | Returns true if the method is abstract. |
IsAssembly | Returns true if the method is visible only to code within the same assembly. |
IsConstructor | Returns true if the method is a constructor. |
IsFamily | Returns true if the method is visible only within this class and derived classes. |
IsFamilyAndAssembly | Returns true if the method is visible only to its family (its class and derived classes) and only within the same assembly. |
IsFamilyOrAssembly | Returns true if the method is visible to its family (its class and derived classes) and to code within the same assembly. |
IsFinal | Returns true if the method is final. |
IsGenericMethod | Returns true if the method is an instance of a generic method. |
IsGenericMethodDefinition | Returns true if the method is a generic method definition. |
IsPrivate | Returns true if the method is private. |
IsPublic | Returns true if the method is public. |
IsStatic | Returns true if the method is static. |
IsVirtual | Returns true if the method is virtual. |
ReturnParameter | Returns a ParameterInfo object representing the method’s return type. |
ReturnType | Returns the method’s return type. |
The following table lists the most useful MethodInfo
methods that are not inherited from the MemberInfo
class.
Method | Description |
CreateDelegate | Returns a delegate for the method. |
GetGenericArguments | Returns an array of Type objects representing the method’s generic arguments. |
GetGenericMethodDefinition | For an instance of a generic method, returns a MethodInfo object representing the method’s generic definition. |
GetParameters | Returns information about the method’s parameters. |
Invoke | Invokes the method, passing it specified parameters. |
MakeGenericMethod | For a generic method definition, returns a MethodInfo representing the method with specific types inserted in place of the generic type parameters. |
A FieldInfo
object contains information about a field. The following table lists the most useful FieldInfo
properties that are not inherited from the MemberInfo
class.
Property | Description |
Attributes | Returns the field’s attributes |
FieldType | Returns the field’s type |
IsAssembly | Returns true if the field is visible only to code within the assembly |
IsFamily | Returns true if the field is visible only to code in its family (its class and derived classes) |
IsFamilyAndAssembly | Returns true if the field is visible only to code in its family that is in the same assembly |
IsFamilyOrAssembly | Returns true if the field is visible to code in its family and to code in the same assembly |
IsInitOnly | Returns true if the field’s value can be set only in the class’s constructor |
IsLiteral | Returns true if the field is initialized at compile time and cannot be changed later |
IsNotSerialized | Returns true if the field is marked with the NotSerialized attribute |
IsPrivate | Returns true if the field is private |
IsPublic | Returns true if the field is public |
IsStatic | Returns true if the field is static |
The following table lists the FieldInfo
class’s most useful methods.
Method | Description |
GetValue | Returns the field’s value |
GetValueDirect | Returns the value of a field supported by a given object |
SetValue | Sets the field’s value |
A PropertyInfo
object contains information about a property. The following table lists the most useful PropertyInfo
properties that are not inherited from the MemberInfo
class.
Property | Description |
Attributes | Returns the property’s attributes |
CanRead | Returns true if the property can be read |
CanWrite | Returns true if the property can be written |
GetMethod | Returns the property’s get accessor |
PropertyType | Returns the property’s type |
SetMethod | Returns the property’s set accessor |
The following table lists the PropertyInfo
class’s most useful methods.
Method | Description |
GetAccessors | Returns an array of MethodInfo objects representing the property’s accessors |
GetGetMethod | Returns a MethodInfo object representing the property’s get accessor |
GetIndexParameters | For indexed properties, returns an array of ParameterInfo objects giving information about the property’s indexes |
GetSetMethod | Returns a MethodInfo object representing the property’s set accessor |
GetValue | Returns the property’s value |
SetValue | Sets the property’s value |
A ParameterInfo
object contains information about a parameter. The following table lists the most useful ParameterInfo
properties.
Property | Description |
Attributes | Returns the parameter’s attributes |
CustomAttributes | Returns the parameter’s custom attributes |
DefaultValue | Returns the parameter’s default value, if it has one |
HasDefaultValue | Returns true if the parameter has a default value |
IsIn | Returns true if the parameter is an input parameter |
IsOptional | Returns true if the parameter is optional |
IsOut | Returns true if the parameter is an output parameter |
Member | Returns the member that uses the parameter |
Name | Returns the parameter’s name |
ParameterType | Returns the parameter’s type |
Position | Returns the parameter’s position (numbered from 0) in a parameter list |
The ParameterInfo
class’s most useful method is GetCustomAttributes
, which returns information about the parameter’s custom attributes.
18.116.29.139