{%Pragmatic Coding%} Tips and tricks for software developers.


What’s new in reflection for .NET 4.5

.NET 4.5 has some changes to the System.Reflection. The most significant is that the Type is now separated into two classes: Type and TypeInfo. TypeInfo object keeps a complete definition, and the Type now keeps only general information. If you use the reflection from your desktop or web applications for NET 4.5, then the old API is still available too, along with new reflections. Today I will focus on how to use some of the main features of the new API. 

Type overview and TypeInfo

Type class provides an overview of the structure of the object.TypeInfo is a complete definition of the object, including its relationship with the parent and inherited classes. Moreover, API Type class has been updated and now returns in the form of a collection of typed IEnumerable, but not like before - arrays. In the case of large and complex assemblies it can read the value of the results of reflection on one, and also simplifies the use of metadata types with LINQ.Windows Store applications only have access to these new IEnumerable collections.

Changes of API Class Type

Let's look at some of the metadata that can be obtained without the use of Type TypeInfo, and then dive into the API TypeInfo. API Type class allows to obtain general information about types. This means that you can get metadata such as name, namespace, the 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 a certain type, you can do the following:

API TypeInfo

As you can see, Type provides an overview of the structure of the class. If you want to dive a little deeper, you can use the new API class TypeInfo. TypeInfo value you can get from Type calling its GetTypeInfo (). Take, for example, the class Person, which contains the name of the properties (FirstName), name (LastName), Modified event and a method Save:

Lets suppose you want to know what properties, methods and events defined in the class Person. This can be easily obtained from the TypeInfo with its properties DeclaredProperties, DeclaredMethods and DeclaredProperties:

Another common problem is a need to find all types within the assembly using reflection. With the updated API System.Reflection, the Assembly class now returns TypeInfo collection instead of an array Type. For example, to get all the types defined in the executing assembly, you can use the property TypeInfo.Assembly, and then read the property on the resulting object DefinedTypes Assembly:

To get a better idea of ​​the new API System.Reflection, let's create a sample application that retrieves a list of all types in the executable assembly. And when we choose the type, we will display the name, full name, properties, methods and events.

Create an application

To begin, create a new C # Windows Store application and add class to Person, as we described earlier. The next step, open the MainPage.xaml and add there next code as the root of Grid.

Now let's update the MainPage class so that he compiled a list of the types defined in the executable assembly.First add using System.Reflection to the top of the file. Then update method OnNavigateTo , it will receive types derived from the application assembly and tied them to the list MyDefinedTypes :

Now all we have to do is to link the event SelectionChanged element MyDefinedTypes handler, which will show the selected TypeInfo Panel TypeInfoDetails :

Finally your MainPage class should looks like this:


Congratulations! Our application is now ready and the following output at run time:

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

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.