Appendix X
Reflection

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.

Type

The following table lists the Type class’s most useful properties.

PropertyDescription
AssemblyGets the assembly where the type is declared.
AttributesGets the type’s attributes.
BaseTypeGets the type’s parent class.
ContainsGenericParametersIndicates whether the type has generic type parameters.
CustomAttributesGets the type’s custom attributes.
FullNameGets the type’s fully qualified name, which includes the type’s namespace but not its assembly.
IsAbstractReturns true if the type is abstract.
IsArrayReturns true if the type is an array.
IsAutoLayoutReturns true if the type’s fields are laid out automatically by the CLR.
IsByRefReturns true if the type is passed by reference.
IsClassReturns true if the type is a class.
IsCOMObjectReturns true if the type is a COM object.
IsEnumReturns true if the type is an enumeration.
IsExplicitLayoutReturns true if the type’s fields are laid out explicitly.
IsGenericTypeGets a value indicating whether the current type is a generic type.
IsImportReturns true if the type was imported from a COM library.
IsInterfaceReturns true if the type is an interface.
IsLayoutSequentialReturns true if the type’s fields are laid out sequentially.
IsMarshalByRefReturns true if the type is marshaled by reference.
IsNestedReturns true if the type’s definition is contained inside another type’s definition.
IsNestedAssemblyReturns true if the type is nested and visible only within its assembly.
IsNestedFamANDAssemReturns true if the type is nested and visible only to classes in its family in its class and derived classes) and its assembly.
IsNestedFamilyReturns true if the type is nested and visible only to classes in its family.
IsNestedFamORAssemReturns true if the type is nested and visible only to classes either in its family or in its assembly.
IsNestedPrivateReturns true if the type is nested and private.
IsNestedPublicReturns true if the type is nested and public.
IsNotPublicReturns true if the type is not public.
IsPointerReturns true if the type is a pointer.
IsPrimitiveReturns true if the type is a primitive type.
IsPublicReturns true if the type is public.
IsSealedReturns true if the type is sealed.
IsSerializableReturns true if the type is serializable.
IsValueTypeReturns true if the type is a value type.
IsVisibleReturns true if the type is visible to code outside the assembly.
ModuleReturns the module where the type is defined.
NameReturns the type’s name.
NamespaceReturns the type’s namespace.
StructLayoutAttributeReturns a StructLayoutAttribute describing the type’s layout.
TypeInitializerGets the type’s initializer.

The following table lists the Type class’s most useful methods.

MethodDescription
FindInterfacesReturns a filtered list of interfaces that the type implements.
FindMembersReturns 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.
GetArrayRankReturns the number of an array’s dimensions.
GetConstructorReturns information about a constructor matching specific parameter types.
GetConstructorsReturns all the type’s public constructors.
GetCustomAttributesReturns an array of the type’s custom attributes.
GetDefaultMembersReturns the type’s default members.
GetElementTypeFor an array, pointer, or reference type, returns the type of items contained by the type.
GetEnumNameFor 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.)
GetEnumNamesFor an enumeration type, returns the names of the enumeration’s values.
GetEnumUnderlyingTypeFor an enumeration type, returns the integer data type used to store the enumeration’s values.
GetEnumValuesFor an enumeration type, returns an array of the integer values used to store the enumeration’s values.
GetEventReturns an EventInfo object representing a specified public event.
GetEventsReturns EventInfo objects representing all the type’s public events.
GetFieldReturns a FieldInfo object representing a specified public field.
GetFieldsReturns FieldInfo objects representing all the type’s public fields.
GetGenericArgumentsReturns an array of type objects representing the type’s generic type arguments.
GetGenericParameterConstraintsReturns an array of type objects representing constraints on a generic type parameter.
GetInterfaceReturns a type representing an interface implemented by the type.
GetInterfacesReturns an array of types representing interfaces implemented by the type.
GetMemberReturns an array of MemberInfo objects containing information about the type’s public members that match search criteria.
GetMembersReturns an array of MemberInfo objects containing information about all the type’s public members.
GetMethodReturns a MethodInfo object representing a public method that matches search criteria.
GetMethodsReturns an array of MethodInfo objects representing the type’s public methods.
GetNestedTypeReturns type information representing a nested type that matches search criteria.
GetNestedTypesReturns an array of type information representing all the type’s nested types.
GetPropertiesReturns an array of PropertyInfo objects representing properties that match search criteria.
GetPropertyReturns a PropertyInfo object representing a property that matches search criteria.
GetTypeThis static method returns a Type object representing a type that matches search criteria.
GetTypeArrayReturns an array of Type objects for objects in an array.
GetTypeFromCLSIDReturns the type with a given class identifier (CLSID) specified as a GUID.
InvokeMemberInvokes a member.
IsAssignableFromReturns true if an instance of the type can be assigned from an instance of another specified type.
IsEnumDefinedFor an enumeration type, returns true if the enumeration defines a specific value.
IsInstanceOfTypeReturns true if a specific object is an instance of the type.
IsSubclassOfReturns true if the type is a subclass of another type.
MakeArrayTypeReturns a new Type object representing an array of the current type.
MakeByRefTypeReturns a new Type object representing the current type when passed by reference.
MakeGenericTypeReturns a new Type object representing the current generic type with specific types substituted in for the generic type parameters.
MakePointerTypeReturns a new Type object representing a pointer to the current type.

MemberInfo

The MemberInfo class is the parent class of EventInfo, MethodInfo, FieldInfo, and PropertyInfo. The following table lists useful MemberInfo properties.

PropertyDescription
CustomAttributesReturns a collection containing information about the member’s custom attributes
DeclaringTypeReturns the class that includes this member
MemberTypeReturns the member’s type
ModuleReturns the module that contains the type that defines this member
NameReturns the member’s name
ReflectedTypeReturns 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.

EventInfo

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.

PropertyDescription
AddMethodReturns the method used to add a delegate to the event
AttributesReturns the event’s attributes
EventHandlerTypeReturns a Type representing the event’s event handler delegate
RaiseMethodReturns the method that is called when the event is raised
RemoveMethodReturns 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.

MethodDescription
AddEventHandlerAdds an event handler to the event
GetAddMethodReturns the method used to add a delegate to the event
GetRaiseMethodReturns the method that is called when the event is raised
GetRemoveMethodReturns the method that removes a delegate from the event handler
RemoveEventHandlerRemoves an event handler from the event

MethodInfo

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.)

PropertyDescription
ContainsGenericParametersReturns true if the method has generic parameters.
IsAbstractReturns true if the method is abstract.
IsAssemblyReturns true if the method is visible only to code within the same assembly.
IsConstructorReturns true if the method is a constructor.
IsFamilyReturns true if the method is visible only within this class and derived classes.
IsFamilyAndAssemblyReturns true if the method is visible only to its family (its class and derived classes) and only within the same assembly.
IsFamilyOrAssemblyReturns true if the method is visible to its family (its class and derived classes) and to code within the same assembly.
IsFinalReturns true if the method is final.
IsGenericMethodReturns true if the method is an instance of a generic method.
IsGenericMethodDefinitionReturns true if the method is a generic method definition.
IsPrivateReturns true if the method is private.
IsPublicReturns true if the method is public.
IsStaticReturns true if the method is static.
IsVirtualReturns true if the method is virtual.
ReturnParameterReturns a ParameterInfo object representing the method’s return type.
ReturnTypeReturns the method’s return type.

The following table lists the most useful MethodInfo methods that are not inherited from the MemberInfo class.

MethodDescription
CreateDelegateReturns a delegate for the method.
GetGenericArgumentsReturns an array of Type objects representing the method’s generic arguments.
GetGenericMethodDefinitionFor an instance of a generic method, returns a MethodInfo object representing the method’s generic definition.
GetParametersReturns information about the method’s parameters.
InvokeInvokes the method, passing it specified parameters.
MakeGenericMethodFor a generic method definition, returns a MethodInfo representing the method with specific types inserted in place of the generic type parameters.

FieldInfo

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.

PropertyDescription
AttributesReturns the field’s attributes
FieldTypeReturns the field’s type
IsAssemblyReturns true if the field is visible only to code within the assembly
IsFamilyReturns true if the field is visible only to code in its family (its class and derived classes)
IsFamilyAndAssemblyReturns true if the field is visible only to code in its family that is in the same assembly
IsFamilyOrAssemblyReturns true if the field is visible to code in its family and to code in the same assembly
IsInitOnlyReturns true if the field’s value can be set only in the class’s constructor
IsLiteralReturns true if the field is initialized at compile time and cannot be changed later
IsNotSerializedReturns true if the field is marked with the NotSerialized attribute
IsPrivateReturns true if the field is private
IsPublicReturns true if the field is public
IsStaticReturns true if the field is static

The following table lists the FieldInfo class’s most useful methods.

MethodDescription
GetValueReturns the field’s value
GetValueDirectReturns the value of a field supported by a given object
SetValueSets the field’s value

PropertyInfo

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.

PropertyDescription
AttributesReturns the property’s attributes
CanReadReturns true if the property can be read
CanWriteReturns true if the property can be written
GetMethodReturns the property’s get accessor
PropertyTypeReturns the property’s type
SetMethodReturns the property’s set accessor

The following table lists the PropertyInfo class’s most useful methods.

MethodDescription
GetAccessorsReturns an array of MethodInfo objects representing the property’s accessors
GetGetMethodReturns a MethodInfo object representing the property’s get accessor
GetIndexParametersFor indexed properties, returns an array of ParameterInfo objects giving information about the property’s indexes
GetSetMethodReturns a MethodInfo object representing the property’s set accessor
GetValueReturns the property’s value
SetValueSets the property’s value

ParameterInfo

A ParameterInfo object contains information about a parameter. The following table lists the most useful ParameterInfo properties.

PropertyDescription
AttributesReturns the parameter’s attributes
CustomAttributesReturns the parameter’s custom attributes
DefaultValueReturns the parameter’s default value, if it has one
HasDefaultValueReturns true if the parameter has a default value
IsInReturns true if the parameter is an input parameter
IsOptionalReturns true if the parameter is optional
IsOutReturns true if the parameter is an output parameter
MemberReturns the member that uses the parameter
NameReturns the parameter’s name
ParameterTypeReturns the parameter’s type
PositionReturns 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.29.139