New in reflection for .NET 4.5
- Transfer

Overview of Type and TypeInfo
The Type class provides a general idea of the structure of an object. TypeInfo, on the other hand, provides a complete definition of an object, including its relationships to parent and inherited classes. Moreover, the API of the Type class has been updated and now returns collections in the form of typed IEnumerable, and not as before - arrays. In the case of large and complex assemblies, this allows you to read the values of the reflection results one at a time, and also simplifies the use of type metadata using LINQ. Windows Store apps only have access to these new IEnumerable collections.
Type Class API Changes
Let's look at some metadata that can be obtained from Type without using TypeInfo, and then dive into the TypeInfo API. The API of the Type class allows you to get general information about types. This means that you can get metadata such as name, namespace, full name, module, and so on. This is done through the same API as in .NET 4.0. For example, to get the full name and namespace of some type, you can do the following:
Type stringType = typeof(string);
string fullName = stringType.FullName;
string stringNameSpace = stringType.Namespace;
TypeInfo API
As you can see, Type provides a general idea of the structure of the class. If you want to dive a little deeper, then you can use the new TypeInfo class API. You can get the TypeInfo value from Type by calling its GetTypeInfo () method. Take, for example, the Person class, which contains the properties of the first name (FirstName), last name (LastName), the Modified event, and the Save method:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public event EventHandler Modified;
public void Save()
{
// сохраняем объект в репозиторий...
}
}
Now let's say you need to know what properties, methods, and events are defined in the Person class. This can be easily learned from TypeInfo using its DeclaredProperties, DeclaredMethods, and DeclaredProperties properties:
TypeInfo personInfo = personType.GetTypeInfo();
IEnumerable declaredProperties = personInfo.DeclaredProperties;
IEnumerable declaredMethods = personInfo.DeclaredMethods;
IEnumerable declaredEvents = personInfo.DeclaredEvents;
Another common task of reflection is the need to find all types within an assembly. Using the updated System.Reflection API, the Assembly class now returns a TypeInfo collection instead of a Type array. For example, to get all types defined in the assembly being executed, you can use the TypeInfo.Assembly property, and then read the DefinedTypes property on the resulting Assembly object:
Assembly myAssembly = this.GetType().GetTypeInfo().Assembly;
IEnumerable myTypes = myAssembly.DefinedTypes;
To get a better idea of the new System.Reflection APIs, let's create an example application that gets a list of all types in the assembly being executed. And when we select a type, we will display its name, full name, properties, methods and events.
Creating an Application
First, create a new C # Windows Store application and add a class for Person there, as we described earlier. In the next step, open MainPage.xaml and add that XAML there as the root element of the Grid.
Now, let's update the MainPage class so that it collects a list of types defined in the assembly being performed. First add
using System.Reflection
to the beginning of the file. Then update the methodOnNavigateTo
so that it receives the received types from the application assembly and binds them to the list MyDefinedTypes
:protected override void OnNavigatedTo(NavigationEventArgs e)
{
Assembly myAssembly = this.GetType().GetTypeInfo().Assembly;
IEnumerable myTypes = myAssembly.DefinedTypes;
MyDefinedTypes.DataContext = myTypes;
}
Now all that remains for us is to associate the event of the
SelectionChanged
element MyDefinedTypes
with a handler that will show the selected TypeInfo in the panel TypeInfoDetails
:private void MyDefinedTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
TypeInfo selectedTypeInfo = e.AddedItems.First() as TypeInfo;
TypeInfoDetails.DataContext = selectedTypeInfo;
TypeInfoDetails.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
In its final form, your MainPage class should look like this .
Congratulations! Our application is ready and now displays the following result at runtime:

As you can see, there are quite significant changes in the API of the Type class. This is especially important if you are upgrading from previous versions of .NET and want to create a Windows Store application in which only the new API is available. This API is also available in regular desktop and .NET 4.5 web applications, as well as in Portable Class Library projects. I believe that the new API has become cleaner thanks to the use of IEnumerable instead of arrays, as well as the separation of general and detailed reflection data into two classes: Type and TypeInfo.