What should I use for reflection? -- Competition between TypeDescriptor family and Type family

Source: Internet
Author: User

 

 

 

As you may know, reflection has two usage methods: TypeDescriptor (including PropertyDescriptor) or Type (including PropertyInfo and other MemberInfo ). But I believe that the vast majority of children's shoes are confused. Why does Microsoft have two kinds of reflection? I am not very clear. What are the differences between the two reflection types? Under what circumstances should they be used? What are their respective performance?

In fact, xuanjicang is in their namespace. The namespace of the Type family is System. Reflection (excluding the Type itself), while the namespace of the TypeDescriptor family is System. ComponentModel. All types in System. Reflection namespace are for Reflection service, while System. ComponentModel is the core namespace of Component, indicating that TypeDescriptor is closely related to Component.

Type is static Type-based reflection. Once a Type is specified, all information about the Type and all members of the Type can be obtained using the Type family, regardless of whether the Member is Public. Therefore, the Type family can meet all runtime reflection requirements.

For TypeDescriptor, we can consider it as follows: TypeDescriptor is the description of Type, and this description is not fixed, it will change and change, so that it is different from the original Type, typeDescriptor can be understood as packaging Type. TypeDescriptor is designed to deal with some requirements during design, because there will be a need to dynamically change the type, members, and attributes during design, but there is almost no such requirement at runtime. In order to dynamically change types, members, and attributes, TypeDescriptor will surely cache some data in the memory. The type cache will exist in a static WeakHashtable. The Caching mechanism of members and attributes is more complex. In addition, TypeDescriptor can only process Public members.

According to the above introduction, we can make a speculation: Because TypeDescriptor needs to establish a cache mechanism and wrap the Type, the performance of TypeDescriptor should be slower than that of Type. However, after the cache is created, repeated use of TypeDescriptor improves the performance.

The following is a simple test program to test the performance of obtaining all attributes:

  

Code

 Class Program
{
Static void Main (string [] args)
{
Int count = 1000000;
TestTypeDescriptorGetProperties (typeof (Component), count );
TestTypeGetProperties (typeof (Component), count );

TestTypeDescriptorGetProperties (typeof (Control), count );
TestTypeGetProperties (typeof (Control), count );
}

Private static void TestTypeDescriptorGetProperties (Type type, params int [] counts)
{
Foreach (int count in counts)
{
Object obj = Activator. CreateInstance (type );
Stopwatch watch = Stopwatch. StartNew ();
Console. Write ("TypeDescriptor. GetProperties (" + type. Name + "type)" + count + "Times :");
For (int I = 0; I <count; I ++)
{
TypeDescriptor. GetProperties (type );
}
Watch. Stop ();
Console. WriteLine (watch. ElapsedMilliseconds );
}
}

Private static void TestTypeGetProperties (Type type, params int [] counts)
{
Foreach (int count in counts)
{
Object obj = Activator. CreateInstance (type );
Stopwatch watch = Stopwatch. StartNew ();
Console. Write ("type. GetProperties (" + type. Name + "type)" + count + "Times :");
For (int I = 0; I <count; I ++)
{
Type. GetProperties ();
}
Watch. Stop ();
Console. WriteLine (watch. ElapsedMilliseconds );
}
}
}

On my local machine, change Count several times and then run the following results:

  

Through the analysis of the results, we can draw the following conclusion:

TypeDescriptor is used for the first time with poor performance. It changes linearly with the increase in the number of times of use. The number of members of the target type has little impact on performance. Type also changes linearly with the increase in the number of times of use, but the number and performance of target Type members also change linearly, the performance of obtaining all the attributes above the Control is about 10 times the performance of obtaining all the attributes on the Component. Of course, in general, we will not use reflection as this order of magnitude, because we can add a cache mechanism in the program to avoid repeated read reflection, in general, we do not need to pay too much attention to the nature of reflection.

To sum up, I think it is best to use Type if we use reflection during runtime, because Type has high performance and can access non-Public members through Type. However, because TypeDescriptor has a more friendly interface, it is more convenient to use, and when TypeDescriptor is used, it is generally unnecessary to catch exceptions, so it doesn't matter if you are used to it.

In design, TypeDescriptor is sometimes required to meet certain requirements. TypeDescriptor is one of the core components of Visual Studio. I will introduce the principles and usage of TypeDescriptor in more articles in the future. Stay tuned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.