Type.Descriptor.GetProperties vs Type.GetProperties

I am looking at code in which the author of MSDN uses the following methods in different methods of the same class:

if ( TypeDescriptor.GetProperties(ModelInstance)[propertyName] != null ) return; var property = ModelInstance.GetType().GetProperty(propertyName); 

Would you use the former because it is faster, and you only need to request the property and the latter if you need to manipulate it? Something else?

+7
source share
1 answer

The first method, as a rule, should not be faster, because by default it uses the second method by default. The TypeDescriptor architecture adds functionality on top of the normal reflection (which is represented by instance.GetType().GetProperty(...) . For more information on the TypeDescriptor architecture, see http://msdn.microsoft.com/en-us/library/ms171819.aspx .

In general, using reflection directly is faster (i.e., your second line above), but there might be a reason to use TypeDescriptor if you are using some kind of custom type provider that can return other results than standard reflection.

+11
source

All Articles