Was this helpful?
This is in continuation to Reflection in .NET - Part 1
Retrieving a Type
Every CLR object or value is an instance of some type. A type itself is an instance of type System.Type. An instance of System.Type can represent any one of the following types: Class, Array, Interface, Enumeration, Value types. This instance encapsulates the information associated with the type being retrieved. It offers several properties and methods. Using them, you can obtain information about the type at runtime. The information can be about its fields, properties, events, constructors, methods, the implementing module, and the container assembly in which the type resides.
If two object references of type Type refer to the same object it means that they represent the same type. The vice-versa also holds true. In case, you wish to ascertain whether two expressions are of the same type, you can compare their Type objects.
If you have an object and you don't know what type it is, you can call the System.Object.GetType() method on it. Recall that the System.Object is a base class for all types and every .NET type inherits from this class. This method returns an instance of the System.Type class. The returned instance represents the type of object on which GetType() is invoked.
The System.Type is the basis for all reflection functions. It is the primary way to retrieve information about the assembly's metadata. Once you obtain the Type of an object, the reflection operations can then be used to access the metadata from the assembly.
As soon as you have the Type instance for a type, you can use these properties to find out whether a given type is a class, an array, an interface, public, sealed, etc: IsClass, IsInterface, IsValueType, IsArray, IsAbstract, etc.
System.Type provides methods such as: GetFields(), GetMethods(), GetConstructors(), GetProperties(), GetInterfaces(), GetEvents(). These are the commonly used methods. Each such method returns a corresponding array that represents the items you are interested in. For example, if you want to obtain a collection of the fields for a given type, you will call GetFields() and it will return an array of FieldInfo objects. Similarly, if you want to obtain a list of the constructors for a given type, you will call GetConstructors() and it will return an array of ConstructorInfo objects.
Now let's say if you are looking for a specific method instead of an array of all methods. Reflection API provides a singular form corresponding to each of these methods that allows you to obtain a specific item that matches the name you specify in the search criteria. Examples include GetMethod(), GetField().
Use the System.Type.GetMembers() method if you want to obtain a list of all the members of a given type. It returns an array of MemberInfo objects that will provide you with basic information about every member of the type.
.NET offers a Type.GetType() method. It is an overloaded public static method that returns an object of type Type.
You can use the Type.FindMembers() method to look for specific members of a type. The method returns a filtered array of MemberInfo objects of the specified member type. For instance, you will use this method if you want to search for public methods whose names begin with 'To'.
The method accepts the following:
a. The type of member you want to search. For example, if you want to search methods, you will specify MemberTypes.Method. If you want to search fields, you will specify MemberTypes.Field where MemberTypes is an enumeration.
b. A bitwise combination of one or more BindingFlags that indicates how the search is to be performed. BindingFlags is discussed later.
c. A delegate that determines if the member currently being examined matches the specified filter criterion. Its objective is to filter the list of members in the array of MemberInfo objects. For example, if you want to filter on name, you'll use the Type.FilterName filter.
d. A filter criterion that is used by the filter. For example, if you're looking for methods whose names begin with 'To', you'll specify To*
If you want to obtain the metadata for a given type and you do not have its object in hand, the System.Object.GetType() won't work in this case because it only operates on living type instances.
The typeof operator lets you obtain the Type object without having to create an instance of the type. It accepts a type as a parameter 'typename' and returns an instance of System.Type that represents the 'typename'.
Consider when you do not have an instance of a Student object in hand. If you want to obtain the System.Type instance that represents the Student type, you cannot use the System.Object.GetType() method because it can only be invoked on an object. In this situation, you can use typeof operator.
Pass to Type.GetType() method the name of your desired type and it will return the associated Type object.
Querying a Type
If you want to query whether a type is a class, an array, serializable, private, and so on, you can use the corresponding public properties on its associated Type object.
Querying for Attributes
Consider a custom attribute named CommentAttribute that can be placed on all target elements.
Now we will use reflection to query the members of the Square type about the attributes attached to it.
Querying the methods for their attached attributes
Consider that you want to know about the attributes that have been placed on a method.
For Part 3, click this link: Reflection in .NET - Part 3
|posted||Apr 19, 2016|
|active||Apr 20, 2016|
|•||Reflection in .NET - Part 5|
|•||Reflection in .NET - Part 4|
|•||Reflection in .NET - Part 3|
|•||Reflection in .NET - Part 1|
|•||.NET Core project option: "Produce outputs on build"|
|•||Fastest way to serialize a POCO to JSON and deserialize JSON to a POCO|
|•||Serialization in .NET - Part 4|
|•||Reading appsettings.json in a .NET Core console application|
|•||Serialization in .NET - Part 3|
|•||Serialization in .NET - Part 2|