Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeDescriptor.GetProperties vs. Type.GetProperties

I'm looking at some code where an MSDN author uses the following 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 its faster and you only need to query a property and the latter if you need to manipulate it? Something else?

like image 811
Berryl Avatar asked Sep 27 '11 18:09

Berryl


1 Answers

The first method should generally not be faster since internally per default it actually uses the second method. The TypeDescriptor architecture adds functionality on top of the normal reflection (which instance.GetType().GetProperty(...) represents. See http://msdn.microsoft.com/en-us/library/ms171819.aspx for more information about the TypeDescriptor architecture.

In general using reflection directly is faster (i.e. your second line above), but there may be a reason for using the TypeDescriptor if some custom type provider is in use which may return other results than the standard reflection.

like image 66
DeCaf Avatar answered Oct 01 '22 07:10

DeCaf